apiDefineComponent.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. import {
  2. ComputedOptions,
  3. MethodOptions,
  4. ComponentOptionsWithoutProps,
  5. ComponentOptionsWithArrayProps,
  6. ComponentOptionsWithObjectProps,
  7. ComponentOptionsMixin,
  8. RenderFunction,
  9. ComponentOptionsBase,
  10. ComponentInjectOptions,
  11. ComponentOptions
  12. } from './componentOptions'
  13. import {
  14. SetupContext,
  15. AllowedComponentProps,
  16. ComponentCustomProps
  17. } from './component'
  18. import {
  19. ExtractPropTypes,
  20. ComponentPropsOptions,
  21. ExtractDefaultPropTypes,
  22. ComponentObjectPropsOptions
  23. } from './componentProps'
  24. import { EmitsOptions, EmitsToProps } from './componentEmits'
  25. import { extend, isFunction } from '@vue/shared'
  26. import { VNodeProps } from './vnode'
  27. import {
  28. CreateComponentPublicInstance,
  29. ComponentPublicInstanceConstructor
  30. } from './componentPublicInstance'
  31. import { SlotsType } from './componentSlots'
  32. export type PublicProps = VNodeProps &
  33. AllowedComponentProps &
  34. ComponentCustomProps
  35. export type DefineComponent<
  36. PropsOrPropOptions = {},
  37. RawBindings = {},
  38. D = {},
  39. C extends ComputedOptions = ComputedOptions,
  40. M extends MethodOptions = MethodOptions,
  41. Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
  42. Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
  43. E extends EmitsOptions = {},
  44. EE extends string = string,
  45. S extends SlotsType = {},
  46. PP = PublicProps,
  47. Props = Readonly<
  48. PropsOrPropOptions extends ComponentPropsOptions
  49. ? ExtractPropTypes<PropsOrPropOptions>
  50. : PropsOrPropOptions
  51. > &
  52. ({} extends E ? {} : EmitsToProps<E>),
  53. Defaults = ExtractDefaultPropTypes<PropsOrPropOptions>
  54. > = ComponentPublicInstanceConstructor<
  55. CreateComponentPublicInstance<
  56. Props,
  57. RawBindings,
  58. D,
  59. C,
  60. M,
  61. Mixin,
  62. Extends,
  63. E,
  64. S,
  65. PP & Props,
  66. Defaults,
  67. true
  68. > &
  69. Props
  70. > &
  71. ComponentOptionsBase<
  72. Props,
  73. RawBindings,
  74. D,
  75. C,
  76. M,
  77. Mixin,
  78. Extends,
  79. E,
  80. EE,
  81. S,
  82. Defaults
  83. > &
  84. PP
  85. // defineComponent is a utility that is primarily used for type inference
  86. // when declaring components. Type inference is provided in the component
  87. // options (provided as the argument). The returned value has artificial types
  88. // for TSX / manual render function / IDE support.
  89. // overload 1: direct setup function
  90. // (uses user defined props interface)
  91. export function defineComponent<
  92. Props extends Record<string, any>,
  93. E extends EmitsOptions = {},
  94. EE extends string = string,
  95. S extends SlotsType = {}
  96. >(
  97. setup: (
  98. props: Props,
  99. ctx: SetupContext<E, S>
  100. ) => RenderFunction | Promise<RenderFunction>,
  101. options?: Pick<ComponentOptions, 'name' | 'inheritAttrs'> & {
  102. props?: (keyof Props)[]
  103. emits?: E | EE[]
  104. slots?: S
  105. }
  106. ): (props: Props & EmitsToProps<E>) => any
  107. export function defineComponent<
  108. Props extends Record<string, any>,
  109. E extends EmitsOptions = {},
  110. EE extends string = string,
  111. S extends SlotsType = {}
  112. >(
  113. setup: (
  114. props: Props,
  115. ctx: SetupContext<E, S>
  116. ) => RenderFunction | Promise<RenderFunction>,
  117. options?: Pick<ComponentOptions, 'name' | 'inheritAttrs'> & {
  118. props?: ComponentObjectPropsOptions<Props>
  119. emits?: E | EE[]
  120. slots?: S
  121. }
  122. ): (props: Props & EmitsToProps<E>) => any
  123. // overload 2: object format with no props
  124. // (uses user defined props interface)
  125. // return type is for Vetur and TSX support
  126. export function defineComponent<
  127. Props = {},
  128. RawBindings = {},
  129. D = {},
  130. C extends ComputedOptions = {},
  131. M extends MethodOptions = {},
  132. Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
  133. Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
  134. E extends EmitsOptions = {},
  135. EE extends string = string,
  136. S extends SlotsType = {},
  137. I extends ComponentInjectOptions = {},
  138. II extends string = string
  139. >(
  140. options: ComponentOptionsWithoutProps<
  141. Props,
  142. RawBindings,
  143. D,
  144. C,
  145. M,
  146. Mixin,
  147. Extends,
  148. E,
  149. EE,
  150. S,
  151. I,
  152. II
  153. >
  154. ): DefineComponent<Props, RawBindings, D, C, M, Mixin, Extends, E, EE, S>
  155. // overload 3: object format with array props declaration
  156. // props inferred as { [key in PropNames]?: any }
  157. // return type is for Vetur and TSX support
  158. export function defineComponent<
  159. PropNames extends string,
  160. RawBindings,
  161. D,
  162. C extends ComputedOptions = {},
  163. M extends MethodOptions = {},
  164. Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
  165. Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
  166. E extends EmitsOptions = {},
  167. EE extends string = string,
  168. S extends SlotsType = {},
  169. I extends ComponentInjectOptions = {},
  170. II extends string = string
  171. >(
  172. options: ComponentOptionsWithArrayProps<
  173. PropNames,
  174. RawBindings,
  175. D,
  176. C,
  177. M,
  178. Mixin,
  179. Extends,
  180. E,
  181. EE,
  182. S,
  183. I,
  184. II
  185. >
  186. ): DefineComponent<
  187. Readonly<{ [key in PropNames]?: any }>,
  188. RawBindings,
  189. D,
  190. C,
  191. M,
  192. Mixin,
  193. Extends,
  194. E,
  195. EE,
  196. S
  197. >
  198. // overload 4: object format with object props declaration
  199. // see `ExtractPropTypes` in ./componentProps.ts
  200. export function defineComponent<
  201. // the Readonly constraint allows TS to treat the type of { required: true }
  202. // as constant instead of boolean.
  203. PropsOptions extends Readonly<ComponentPropsOptions>,
  204. RawBindings,
  205. D,
  206. C extends ComputedOptions = {},
  207. M extends MethodOptions = {},
  208. Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
  209. Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
  210. E extends EmitsOptions = {},
  211. EE extends string = string,
  212. S extends SlotsType = {},
  213. I extends ComponentInjectOptions = {},
  214. II extends string = string
  215. >(
  216. options: ComponentOptionsWithObjectProps<
  217. PropsOptions,
  218. RawBindings,
  219. D,
  220. C,
  221. M,
  222. Mixin,
  223. Extends,
  224. E,
  225. EE,
  226. S,
  227. I,
  228. II
  229. >
  230. ): DefineComponent<PropsOptions, RawBindings, D, C, M, Mixin, Extends, E, EE, S>
  231. // implementation, close to no-op
  232. export function defineComponent(
  233. options: unknown,
  234. extraOptions?: ComponentOptions
  235. ) {
  236. return isFunction(options)
  237. ? extend({}, extraOptions, { setup: options, name: options.name })
  238. : options
  239. }