apiCreateComponent.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import {
  2. ComputedOptions,
  3. MethodOptions,
  4. ComponentOptionsWithoutProps,
  5. ComponentOptionsWithArrayProps,
  6. ComponentOptionsWithProps
  7. } from './componentOptions'
  8. import { SetupContext } from './component'
  9. import { VNodeChild } from './vnode'
  10. import { ComponentPublicInstance } from './componentPublicInstanceProxy'
  11. import { ExtractPropTypes } from './componentProps'
  12. import { isFunction } from '@vue/shared'
  13. // overload 1: direct setup function
  14. // (uses user defined props interface)
  15. export function createComponent<Props>(
  16. setup: (props: Props, ctx: SetupContext) => object | (() => VNodeChild)
  17. ): (props: Props) => any
  18. // overload 2: object format with no props
  19. // (uses user defined props interface)
  20. // return type is for Vetur and TSX support
  21. export function createComponent<
  22. Props,
  23. RawBindings,
  24. D,
  25. C extends ComputedOptions = {},
  26. M extends MethodOptions = {}
  27. >(
  28. options: ComponentOptionsWithoutProps<Props, RawBindings, D, C, M>
  29. ): {
  30. new (): ComponentPublicInstance<Props, RawBindings, D, C, M>
  31. }
  32. // overload 3: object format with array props declaration
  33. // props inferred as { [key in PropNames]?: unknown }
  34. // return type is for Vetur and TSX support
  35. export function createComponent<
  36. PropNames extends string,
  37. RawBindings,
  38. D,
  39. C extends ComputedOptions = {},
  40. M extends MethodOptions = {}
  41. >(
  42. options: ComponentOptionsWithArrayProps<PropNames, RawBindings, D, C, M>
  43. ): {
  44. new (): ComponentPublicInstance<
  45. { [key in PropNames]?: unknown },
  46. RawBindings,
  47. D,
  48. C,
  49. M
  50. >
  51. }
  52. // overload 4: object format with object props declaration
  53. // see `ExtractPropTypes` in ./componentProps.ts
  54. export function createComponent<
  55. PropsOptions,
  56. RawBindings,
  57. D,
  58. C extends ComputedOptions = {},
  59. M extends MethodOptions = {}
  60. >(
  61. options: ComponentOptionsWithProps<PropsOptions, RawBindings, D, C, M>
  62. ): {
  63. // for Vetur and TSX support
  64. new (): ComponentPublicInstance<
  65. ExtractPropTypes<PropsOptions>,
  66. RawBindings,
  67. D,
  68. C,
  69. M,
  70. ExtractPropTypes<PropsOptions, false>
  71. >
  72. }
  73. // implementation, close to no-op
  74. export function createComponent(options: any) {
  75. return isFunction(options) ? { setup: options } : (options as any)
  76. }