apiCreateComponent.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import {
  2. ComputedOptions,
  3. MethodOptions,
  4. ComponentOptionsWithoutProps,
  5. ComponentOptionsWithArrayProps,
  6. ComponentOptionsWithObjectProps
  7. } from './apiOptions'
  8. import { SetupContext, RenderFunction } from './component'
  9. import { ComponentPublicInstance } from './componentProxy'
  10. import { ExtractPropTypes, ComponentPropsOptions } from './componentProps'
  11. import { isFunction } from '@vue/shared'
  12. import { VNodeProps } from './vnode'
  13. // overload 1: direct setup function
  14. // (uses user defined props interface)
  15. export function createComponent<Props, RawBindings = object>(
  16. setup: (props: Props, ctx: SetupContext) => RawBindings | RenderFunction
  17. ): {
  18. new (): ComponentPublicInstance<
  19. Props,
  20. RawBindings,
  21. {},
  22. {},
  23. {},
  24. // public props
  25. VNodeProps & Props
  26. >
  27. }
  28. // overload 2: object format with no props
  29. // (uses user defined props interface)
  30. // return type is for Vetur and TSX support
  31. export function createComponent<
  32. Props,
  33. RawBindings,
  34. D,
  35. C extends ComputedOptions = {},
  36. M extends MethodOptions = {}
  37. >(
  38. options: ComponentOptionsWithoutProps<Props, RawBindings, D, C, M>
  39. ): {
  40. new (): ComponentPublicInstance<
  41. Props,
  42. RawBindings,
  43. D,
  44. C,
  45. M,
  46. VNodeProps & Props
  47. >
  48. }
  49. // overload 3: object format with array props declaration
  50. // props inferred as { [key in PropNames]?: any }
  51. // return type is for Vetur and TSX support
  52. export function createComponent<
  53. PropNames extends string,
  54. RawBindings,
  55. D,
  56. C extends ComputedOptions = {},
  57. M extends MethodOptions = {}
  58. >(
  59. options: ComponentOptionsWithArrayProps<PropNames, RawBindings, D, C, M>
  60. ): {
  61. // array props technically doesn't place any contraints on props in TSX
  62. new (): ComponentPublicInstance<VNodeProps, RawBindings, D, C, M>
  63. }
  64. // overload 4: object format with object props declaration
  65. // see `ExtractPropTypes` in ./componentProps.ts
  66. export function createComponent<
  67. // the Readonly constraint allows TS to treat the type of { required: true }
  68. // as constant instead of boolean.
  69. PropsOptions extends Readonly<ComponentPropsOptions>,
  70. RawBindings,
  71. D,
  72. C extends ComputedOptions = {},
  73. M extends MethodOptions = {}
  74. >(
  75. options: ComponentOptionsWithObjectProps<PropsOptions, RawBindings, D, C, M>
  76. ): {
  77. // for Vetur and TSX support
  78. new (): ComponentPublicInstance<
  79. ExtractPropTypes<PropsOptions>,
  80. RawBindings,
  81. D,
  82. C,
  83. M,
  84. VNodeProps & ExtractPropTypes<PropsOptions, false>
  85. >
  86. }
  87. // implementation, close to no-op
  88. export function createComponent(options: unknown) {
  89. return isFunction(options) ? { setup: options } : options
  90. }