apiDefineComponent.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. type ResolveProps<PropsOrPropOptions, E extends EmitsOptions> = Readonly<
  36. PropsOrPropOptions extends ComponentPropsOptions
  37. ? ExtractPropTypes<PropsOrPropOptions>
  38. : PropsOrPropOptions
  39. > &
  40. ({} extends E ? {} : EmitsToProps<E>)
  41. export type DefineComponent<
  42. PropsOrPropOptions = {},
  43. RawBindings = {},
  44. D = {},
  45. C extends ComputedOptions = ComputedOptions,
  46. M extends MethodOptions = MethodOptions,
  47. Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
  48. Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
  49. E extends EmitsOptions = {},
  50. EE extends string = string,
  51. PP = PublicProps,
  52. Props = ResolveProps<PropsOrPropOptions, E>,
  53. Defaults = ExtractDefaultPropTypes<PropsOrPropOptions>,
  54. S extends SlotsType = {}
  55. > = ComponentPublicInstanceConstructor<
  56. CreateComponentPublicInstance<
  57. Props,
  58. RawBindings,
  59. D,
  60. C,
  61. M,
  62. Mixin,
  63. Extends,
  64. E,
  65. PP & Props,
  66. Defaults,
  67. true,
  68. {},
  69. S
  70. > &
  71. Props
  72. > &
  73. ComponentOptionsBase<
  74. Props,
  75. RawBindings,
  76. D,
  77. C,
  78. M,
  79. Mixin,
  80. Extends,
  81. E,
  82. EE,
  83. Defaults,
  84. {},
  85. string,
  86. S
  87. > &
  88. PP
  89. // defineComponent is a utility that is primarily used for type inference
  90. // when declaring components. Type inference is provided in the component
  91. // options (provided as the argument). The returned value has artificial types
  92. // for TSX / manual render function / IDE support.
  93. // overload 1: direct setup function
  94. // (uses user defined props interface)
  95. export function defineComponent<
  96. Props extends Record<string, any>,
  97. E extends EmitsOptions = {},
  98. EE extends string = string,
  99. S extends SlotsType = {}
  100. >(
  101. setup: (
  102. props: Props,
  103. ctx: SetupContext<E, S>
  104. ) => RenderFunction | Promise<RenderFunction>,
  105. options?: Pick<ComponentOptions, 'name' | 'inheritAttrs'> & {
  106. props?: (keyof Props)[]
  107. emits?: E | EE[]
  108. slots?: S
  109. }
  110. ): (props: Props & EmitsToProps<E>) => any
  111. export function defineComponent<
  112. Props extends Record<string, any>,
  113. E extends EmitsOptions = {},
  114. EE extends string = string,
  115. S extends SlotsType = {}
  116. >(
  117. setup: (
  118. props: Props,
  119. ctx: SetupContext<E, S>
  120. ) => RenderFunction | Promise<RenderFunction>,
  121. options?: Pick<ComponentOptions, 'name' | 'inheritAttrs'> & {
  122. props?: ComponentObjectPropsOptions<Props>
  123. emits?: E | EE[]
  124. slots?: S
  125. }
  126. ): (props: Props & EmitsToProps<E>) => any
  127. // overload 2: object format with no props
  128. // (uses user defined props interface)
  129. // return type is for Vetur and TSX support
  130. export function defineComponent<
  131. Props = {},
  132. RawBindings = {},
  133. D = {},
  134. C extends ComputedOptions = {},
  135. M extends MethodOptions = {},
  136. Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
  137. Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
  138. E extends EmitsOptions = {},
  139. EE extends string = string,
  140. S extends SlotsType = {},
  141. I extends ComponentInjectOptions = {},
  142. II extends string = string
  143. >(
  144. options: ComponentOptionsWithoutProps<
  145. Props,
  146. RawBindings,
  147. D,
  148. C,
  149. M,
  150. Mixin,
  151. Extends,
  152. E,
  153. EE,
  154. I,
  155. II,
  156. S
  157. >
  158. ): DefineComponent<
  159. Props,
  160. RawBindings,
  161. D,
  162. C,
  163. M,
  164. Mixin,
  165. Extends,
  166. E,
  167. EE,
  168. PublicProps,
  169. ResolveProps<Props, E>,
  170. ExtractDefaultPropTypes<Props>,
  171. S
  172. >
  173. // overload 3: object format with array props declaration
  174. // props inferred as { [key in PropNames]?: any }
  175. // return type is for Vetur and TSX support
  176. export function defineComponent<
  177. PropNames extends string,
  178. RawBindings,
  179. D,
  180. C extends ComputedOptions = {},
  181. M extends MethodOptions = {},
  182. Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
  183. Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
  184. E extends EmitsOptions = {},
  185. EE extends string = string,
  186. S extends SlotsType = {},
  187. I extends ComponentInjectOptions = {},
  188. II extends string = string,
  189. Props = Readonly<{ [key in PropNames]?: any }>
  190. >(
  191. options: ComponentOptionsWithArrayProps<
  192. PropNames,
  193. RawBindings,
  194. D,
  195. C,
  196. M,
  197. Mixin,
  198. Extends,
  199. E,
  200. EE,
  201. I,
  202. II,
  203. S
  204. >
  205. ): DefineComponent<
  206. Props,
  207. RawBindings,
  208. D,
  209. C,
  210. M,
  211. Mixin,
  212. Extends,
  213. E,
  214. EE,
  215. PublicProps,
  216. ResolveProps<Props, E>,
  217. ExtractDefaultPropTypes<Props>,
  218. S
  219. >
  220. // overload 4: object format with object props declaration
  221. // see `ExtractPropTypes` in ./componentProps.ts
  222. export function defineComponent<
  223. // the Readonly constraint allows TS to treat the type of { required: true }
  224. // as constant instead of boolean.
  225. PropsOptions extends Readonly<ComponentPropsOptions>,
  226. RawBindings,
  227. D,
  228. C extends ComputedOptions = {},
  229. M extends MethodOptions = {},
  230. Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
  231. Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
  232. E extends EmitsOptions = {},
  233. EE extends string = string,
  234. S extends SlotsType = {},
  235. I extends ComponentInjectOptions = {},
  236. II extends string = string
  237. >(
  238. options: ComponentOptionsWithObjectProps<
  239. PropsOptions,
  240. RawBindings,
  241. D,
  242. C,
  243. M,
  244. Mixin,
  245. Extends,
  246. E,
  247. EE,
  248. I,
  249. II,
  250. S
  251. >
  252. ): DefineComponent<
  253. PropsOptions,
  254. RawBindings,
  255. D,
  256. C,
  257. M,
  258. Mixin,
  259. Extends,
  260. E,
  261. EE,
  262. PublicProps,
  263. ResolveProps<PropsOptions, E>,
  264. ExtractDefaultPropTypes<PropsOptions>,
  265. S
  266. >
  267. // implementation, close to no-op
  268. export function defineComponent(
  269. options: unknown,
  270. extraOptions?: ComponentOptions
  271. ) {
  272. return isFunction(options)
  273. ? // #8326: extend call and options.name access are considered side-effects
  274. // by Rollup, so we have to wrap it in a pure-annotated IIFE.
  275. /*#__PURE__*/ (() =>
  276. extend({ name: options.name }, extraOptions, { setup: options }))()
  277. : options
  278. }