apiDefineComponent.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import {
  2. ComputedOptions,
  3. MethodOptions,
  4. ComponentOptionsWithoutProps,
  5. ComponentOptionsWithArrayProps,
  6. ComponentOptionsWithObjectProps,
  7. ComponentOptionsMixin,
  8. RenderFunction,
  9. ComponentOptionsBase
  10. } from './componentOptions'
  11. import {
  12. SetupContext,
  13. AllowedComponentProps,
  14. ComponentCustomProps
  15. } from './component'
  16. import {
  17. ExtractPropTypes,
  18. ComponentPropsOptions,
  19. ExtractDefaultPropTypes
  20. } from './componentProps'
  21. import { EmitsOptions, EmitsToProps } from './componentEmits'
  22. import { isFunction } from '@vue/shared'
  23. import { VNodeProps } from './vnode'
  24. import {
  25. CreateComponentPublicInstance,
  26. ComponentPublicInstanceConstructor
  27. } from './componentPublicInstance'
  28. export type PublicProps = VNodeProps &
  29. AllowedComponentProps &
  30. ComponentCustomProps
  31. export type DefineComponent<
  32. PropsOrPropOptions = {},
  33. RawBindings = {},
  34. D = {},
  35. C extends ComputedOptions = ComputedOptions,
  36. M extends MethodOptions = MethodOptions,
  37. Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
  38. Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
  39. E extends EmitsOptions = Record<string, any>,
  40. EE extends string = string,
  41. PP = PublicProps,
  42. Props = Readonly<ExtractPropTypes<PropsOrPropOptions>> & EmitsToProps<E>,
  43. Defaults = ExtractDefaultPropTypes<PropsOrPropOptions>
  44. > = ComponentPublicInstanceConstructor<
  45. CreateComponentPublicInstance<
  46. Props,
  47. RawBindings,
  48. D,
  49. C,
  50. M,
  51. Mixin,
  52. Extends,
  53. E,
  54. PP & Props,
  55. Defaults,
  56. true
  57. > &
  58. Props
  59. > &
  60. ComponentOptionsBase<
  61. Props,
  62. RawBindings,
  63. D,
  64. C,
  65. M,
  66. Mixin,
  67. Extends,
  68. E,
  69. EE,
  70. Defaults
  71. > &
  72. PP
  73. // defineComponent is a utility that is primarily used for type inference
  74. // when declaring components. Type inference is provided in the component
  75. // options (provided as the argument). The returned value has artificial types
  76. // for TSX / manual render function / IDE support.
  77. // overload 1: direct setup function
  78. // (uses user defined props interface)
  79. export function defineComponent<Props, RawBindings = object>(
  80. setup: (
  81. props: Readonly<Props>,
  82. ctx: SetupContext
  83. ) => RawBindings | RenderFunction
  84. ): DefineComponent<Props, RawBindings>
  85. // overload 2: object format with no props
  86. // (uses user defined props interface)
  87. // return type is for Vetur and TSX support
  88. export function defineComponent<
  89. Props = {},
  90. RawBindings = {},
  91. D = {},
  92. C extends ComputedOptions = {},
  93. M extends MethodOptions = {},
  94. Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
  95. Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
  96. E extends EmitsOptions = EmitsOptions,
  97. EE extends string = string
  98. >(
  99. options: ComponentOptionsWithoutProps<
  100. Props & EmitsToProps<E>,
  101. RawBindings,
  102. D,
  103. C,
  104. M,
  105. Mixin,
  106. Extends,
  107. E,
  108. EE
  109. >
  110. ): DefineComponent<Props, RawBindings, D, C, M, Mixin, Extends, E, EE>
  111. // overload 3: object format with array props declaration
  112. // props inferred as { [key in PropNames]?: any }
  113. // return type is for Vetur and TSX support
  114. export function defineComponent<
  115. PropNames extends string,
  116. RawBindings,
  117. D,
  118. C extends ComputedOptions = {},
  119. M extends MethodOptions = {},
  120. Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
  121. Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
  122. E extends EmitsOptions = Record<string, any>,
  123. EE extends string = string
  124. >(
  125. options: ComponentOptionsWithArrayProps<
  126. PropNames,
  127. RawBindings,
  128. D,
  129. C,
  130. M,
  131. Mixin,
  132. Extends,
  133. E,
  134. EE
  135. >
  136. ): DefineComponent<
  137. Readonly<{ [key in PropNames]?: any }>,
  138. RawBindings,
  139. D,
  140. C,
  141. M,
  142. Mixin,
  143. Extends,
  144. E,
  145. EE
  146. >
  147. // overload 4: object format with object props declaration
  148. // see `ExtractPropTypes` in ./componentProps.ts
  149. export function defineComponent<
  150. // the Readonly constraint allows TS to treat the type of { required: true }
  151. // as constant instead of boolean.
  152. PropsOptions extends Readonly<ComponentPropsOptions>,
  153. RawBindings,
  154. D,
  155. C extends ComputedOptions = {},
  156. M extends MethodOptions = {},
  157. Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
  158. Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
  159. E extends EmitsOptions = Record<string, any>,
  160. EE extends string = string
  161. >(
  162. options: ComponentOptionsWithObjectProps<
  163. PropsOptions,
  164. RawBindings,
  165. D,
  166. C,
  167. M,
  168. Mixin,
  169. Extends,
  170. E,
  171. EE
  172. >
  173. ): DefineComponent<PropsOptions, RawBindings, D, C, M, Mixin, Extends, E, EE>
  174. // implementation, close to no-op
  175. export function defineComponent(options: unknown) {
  176. return isFunction(options) ? { setup: options, name: options.name } : options
  177. }