apiDefineComponent.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. import type {
  2. ComponentInjectOptions,
  3. ComponentOptions,
  4. ComponentOptionsBase,
  5. ComponentOptionsMixin,
  6. ComponentOptionsWithArrayProps,
  7. ComponentOptionsWithObjectProps,
  8. ComponentOptionsWithoutProps,
  9. ComputedOptions,
  10. MethodOptions,
  11. RenderFunction,
  12. } from './componentOptions'
  13. import type {
  14. AllowedComponentProps,
  15. ComponentCustomProps,
  16. SetupContext,
  17. } from './component'
  18. import type {
  19. ComponentObjectPropsOptions,
  20. ComponentPropsOptions,
  21. ExtractDefaultPropTypes,
  22. ExtractPropTypes,
  23. } from './componentProps'
  24. import type { EmitsOptions, EmitsToProps } from './componentEmits'
  25. import { extend, isFunction } from '@vue/shared'
  26. import type { VNodeProps } from './vnode'
  27. import type {
  28. ComponentPublicInstanceConstructor,
  29. CreateComponentPublicInstance,
  30. } from './componentPublicInstance'
  31. import type { 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. > &
  72. ComponentOptionsBase<
  73. Props,
  74. RawBindings,
  75. D,
  76. C,
  77. M,
  78. Mixin,
  79. Extends,
  80. E,
  81. EE,
  82. Defaults,
  83. {},
  84. string,
  85. S
  86. > &
  87. PP
  88. export type DefineSetupFnComponent<
  89. P extends Record<string, any>,
  90. E extends EmitsOptions = {},
  91. S extends SlotsType = SlotsType,
  92. Props = P & EmitsToProps<E>,
  93. PP = PublicProps,
  94. > = new (
  95. props: Props & PP,
  96. ) => CreateComponentPublicInstance<
  97. Props,
  98. {},
  99. {},
  100. {},
  101. {},
  102. ComponentOptionsMixin,
  103. ComponentOptionsMixin,
  104. E,
  105. PP,
  106. {},
  107. false,
  108. {},
  109. S
  110. >
  111. // defineComponent is a utility that is primarily used for type inference
  112. // when declaring components. Type inference is provided in the component
  113. // options (provided as the argument). The returned value has artificial types
  114. // for TSX / manual render function / IDE support.
  115. // overload 1: direct setup function
  116. // (uses user defined props interface)
  117. export function defineComponent<
  118. Props extends Record<string, any>,
  119. E extends EmitsOptions = {},
  120. EE extends string = string,
  121. S extends SlotsType = {},
  122. >(
  123. setup: (
  124. props: Props,
  125. ctx: SetupContext<E, S>,
  126. ) => RenderFunction | Promise<RenderFunction>,
  127. options?: Pick<ComponentOptions, 'name' | 'inheritAttrs'> & {
  128. props?: (keyof Props)[]
  129. emits?: E | EE[]
  130. slots?: S
  131. },
  132. ): DefineSetupFnComponent<Props, E, S>
  133. export function defineComponent<
  134. Props extends Record<string, any>,
  135. E extends EmitsOptions = {},
  136. EE extends string = string,
  137. S extends SlotsType = {},
  138. >(
  139. setup: (
  140. props: Props,
  141. ctx: SetupContext<E, S>,
  142. ) => RenderFunction | Promise<RenderFunction>,
  143. options?: Pick<ComponentOptions, 'name' | 'inheritAttrs'> & {
  144. props?: ComponentObjectPropsOptions<Props>
  145. emits?: E | EE[]
  146. slots?: S
  147. },
  148. ): DefineSetupFnComponent<Props, E, S>
  149. // overload 2: object format with no props
  150. // (uses user defined props interface)
  151. // return type is for Vetur and TSX support
  152. export function defineComponent<
  153. Props = {},
  154. RawBindings = {},
  155. D = {},
  156. C extends ComputedOptions = {},
  157. M extends MethodOptions = {},
  158. Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
  159. Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
  160. E extends EmitsOptions = {},
  161. EE extends string = string,
  162. S extends SlotsType = {},
  163. I extends ComponentInjectOptions = {},
  164. II extends string = string,
  165. >(
  166. options: ComponentOptionsWithoutProps<
  167. Props,
  168. RawBindings,
  169. D,
  170. C,
  171. M,
  172. Mixin,
  173. Extends,
  174. E,
  175. EE,
  176. I,
  177. II,
  178. S
  179. >,
  180. ): DefineComponent<
  181. Props,
  182. RawBindings,
  183. D,
  184. C,
  185. M,
  186. Mixin,
  187. Extends,
  188. E,
  189. EE,
  190. PublicProps,
  191. ResolveProps<Props, E>,
  192. ExtractDefaultPropTypes<Props>,
  193. S
  194. >
  195. // overload 3: object format with array props declaration
  196. // props inferred as { [key in PropNames]?: any }
  197. // return type is for Vetur and TSX support
  198. export function defineComponent<
  199. PropNames extends string,
  200. RawBindings,
  201. D,
  202. C extends ComputedOptions = {},
  203. M extends MethodOptions = {},
  204. Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
  205. Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
  206. E extends EmitsOptions = {},
  207. EE extends string = string,
  208. S extends SlotsType = {},
  209. I extends ComponentInjectOptions = {},
  210. II extends string = string,
  211. Props = Readonly<{ [key in PropNames]?: any }>,
  212. >(
  213. options: ComponentOptionsWithArrayProps<
  214. PropNames,
  215. RawBindings,
  216. D,
  217. C,
  218. M,
  219. Mixin,
  220. Extends,
  221. E,
  222. EE,
  223. I,
  224. II,
  225. S
  226. >,
  227. ): DefineComponent<
  228. Props,
  229. RawBindings,
  230. D,
  231. C,
  232. M,
  233. Mixin,
  234. Extends,
  235. E,
  236. EE,
  237. PublicProps,
  238. ResolveProps<Props, E>,
  239. ExtractDefaultPropTypes<Props>,
  240. S
  241. >
  242. // overload 4: object format with object props declaration
  243. // see `ExtractPropTypes` in ./componentProps.ts
  244. export function defineComponent<
  245. // the Readonly constraint allows TS to treat the type of { required: true }
  246. // as constant instead of boolean.
  247. PropsOptions extends Readonly<ComponentPropsOptions>,
  248. RawBindings,
  249. D,
  250. C extends ComputedOptions = {},
  251. M extends MethodOptions = {},
  252. Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
  253. Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
  254. E extends EmitsOptions = {},
  255. EE extends string = string,
  256. S extends SlotsType = {},
  257. I extends ComponentInjectOptions = {},
  258. II extends string = string,
  259. >(
  260. options: ComponentOptionsWithObjectProps<
  261. PropsOptions,
  262. RawBindings,
  263. D,
  264. C,
  265. M,
  266. Mixin,
  267. Extends,
  268. E,
  269. EE,
  270. I,
  271. II,
  272. S
  273. >,
  274. ): DefineComponent<
  275. PropsOptions,
  276. RawBindings,
  277. D,
  278. C,
  279. M,
  280. Mixin,
  281. Extends,
  282. E,
  283. EE,
  284. PublicProps,
  285. ResolveProps<PropsOptions, E>,
  286. ExtractDefaultPropTypes<PropsOptions>,
  287. S
  288. >
  289. // implementation, close to no-op
  290. /*! #__NO_SIDE_EFFECTS__ */
  291. export function defineComponent(
  292. options: unknown,
  293. extraOptions?: ComponentOptions,
  294. ) {
  295. return isFunction(options)
  296. ? // #8326: extend call and options.name access are considered side-effects
  297. // by Rollup, so we have to wrap it in a pure-annotated IIFE.
  298. /*#__PURE__*/ (() =>
  299. extend({ name: options.name }, extraOptions, { setup: options }))()
  300. : options
  301. }