apiCreateComponent.ts 2.6 KB

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