v3-component-options.d.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. import { Vue } from './vue'
  2. import { VNode } from './vnode'
  3. import { ComponentOptions as Vue2ComponentOptions } from './options'
  4. import { EmitsOptions, SetupContext } from './v3-setup-context'
  5. import { Data, LooseRequired, UnionToIntersection } from './common'
  6. import {
  7. ComponentPropsOptions,
  8. ExtractDefaultPropTypes,
  9. ExtractPropTypes
  10. } from './v3-component-props'
  11. import { CreateComponentPublicInstance } from './v3-component-public-instance'
  12. export { ComponentPropsOptions } from './v3-component-props'
  13. /**
  14. * Interface for declaring custom options.
  15. *
  16. * @example
  17. * ```ts
  18. * declare module 'vue' {
  19. * interface ComponentCustomOptions {
  20. * beforeRouteUpdate?(
  21. * to: Route,
  22. * from: Route,
  23. * next: () => void
  24. * ): void
  25. * }
  26. * }
  27. * ```
  28. */
  29. export interface ComponentCustomOptions {}
  30. export type ComputedGetter<T> = (ctx?: any) => T
  31. export type ComputedSetter<T> = (v: T) => void
  32. export interface WritableComputedOptions<T> {
  33. get: ComputedGetter<T>
  34. set: ComputedSetter<T>
  35. }
  36. export type ComputedOptions = Record<
  37. string,
  38. ComputedGetter<any> | WritableComputedOptions<any>
  39. >
  40. export interface MethodOptions {
  41. [key: string]: Function
  42. }
  43. export type SetupFunction<
  44. Props,
  45. RawBindings = {},
  46. Emits extends EmitsOptions = {}
  47. > = (
  48. this: void,
  49. props: Readonly<Props>,
  50. ctx: SetupContext<Emits>
  51. ) => RawBindings | (() => VNode | null) | void
  52. type ExtractOptionProp<T> = T extends ComponentOptionsBase<
  53. infer P, // Props
  54. any, // RawBindings
  55. any, // D
  56. any, // C
  57. any, // M
  58. any, // Mixin
  59. any, // Extends
  60. any, // EmitsOptions
  61. any // Defaults
  62. >
  63. ? unknown extends P
  64. ? {}
  65. : P
  66. : {}
  67. export interface ComponentOptionsBase<
  68. Props,
  69. RawBindings,
  70. D,
  71. C extends ComputedOptions,
  72. M extends MethodOptions,
  73. Mixin extends ComponentOptionsMixin,
  74. Extends extends ComponentOptionsMixin,
  75. Emits extends EmitsOptions,
  76. EmitNames extends string = string,
  77. Defaults = {}
  78. > extends Omit<
  79. Vue2ComponentOptions<Vue, D, M, C, Props>,
  80. 'data' | 'computed' | 'methods' | 'setup' | 'props' | 'mixins' | 'extends'
  81. >,
  82. ComponentCustomOptions {
  83. // rewrite options api types
  84. data?: (
  85. this: CreateComponentPublicInstance<Props, {}, {}, {}, M, Mixin, Extends>,
  86. vm: CreateComponentPublicInstance<Props, {}, {}, {}, M, Mixin, Extends>
  87. ) => D
  88. computed?: C
  89. methods?: M
  90. mixins?: Mixin[]
  91. extends?: Extends
  92. emits?: (Emits | EmitNames[]) & ThisType<void>
  93. setup?: SetupFunction<
  94. Readonly<
  95. LooseRequired<
  96. Props &
  97. UnionToIntersection<ExtractOptionProp<Mixin>> &
  98. UnionToIntersection<ExtractOptionProp<Extends>>
  99. >
  100. >,
  101. RawBindings,
  102. Emits
  103. >
  104. __defaults?: Defaults
  105. }
  106. export type ComponentOptionsMixin = ComponentOptionsBase<
  107. any,
  108. any,
  109. any,
  110. any,
  111. any,
  112. any,
  113. any,
  114. any,
  115. any,
  116. any
  117. >
  118. export type ExtractComputedReturns<T extends any> = {
  119. [key in keyof T]: T[key] extends { get: (...args: any[]) => infer TReturn }
  120. ? TReturn
  121. : T[key] extends (...args: any[]) => infer TReturn
  122. ? TReturn
  123. : never
  124. }
  125. export type ComponentOptionsWithProps<
  126. PropsOptions = ComponentPropsOptions,
  127. RawBindings = Data,
  128. D = Data,
  129. C extends ComputedOptions = {},
  130. M extends MethodOptions = {},
  131. Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
  132. Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
  133. Emits extends EmitsOptions = {},
  134. EmitsNames extends string = string,
  135. Props = ExtractPropTypes<PropsOptions>,
  136. Defaults = ExtractDefaultPropTypes<PropsOptions>
  137. > = ComponentOptionsBase<
  138. Props,
  139. RawBindings,
  140. D,
  141. C,
  142. M,
  143. Mixin,
  144. Extends,
  145. Emits,
  146. EmitsNames,
  147. Defaults
  148. > & {
  149. props?: PropsOptions
  150. } & ThisType<
  151. CreateComponentPublicInstance<
  152. Props,
  153. RawBindings,
  154. D,
  155. C,
  156. M,
  157. Mixin,
  158. Extends,
  159. Emits
  160. >
  161. >
  162. export type ComponentOptionsWithArrayProps<
  163. PropNames extends string = string,
  164. RawBindings = Data,
  165. D = Data,
  166. C extends ComputedOptions = {},
  167. M extends MethodOptions = {},
  168. Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
  169. Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
  170. Emits extends EmitsOptions = {},
  171. EmitsNames extends string = string,
  172. Props = Readonly<{ [key in PropNames]?: any }>
  173. > = ComponentOptionsBase<
  174. Props,
  175. RawBindings,
  176. D,
  177. C,
  178. M,
  179. Mixin,
  180. Extends,
  181. Emits,
  182. EmitsNames,
  183. {}
  184. > & {
  185. props?: PropNames[]
  186. } & ThisType<
  187. CreateComponentPublicInstance<
  188. Props,
  189. RawBindings,
  190. D,
  191. C,
  192. M,
  193. Mixin,
  194. Extends,
  195. Emits
  196. >
  197. >
  198. export type ComponentOptionsWithoutProps<
  199. Props = {},
  200. RawBindings = Data,
  201. D = Data,
  202. C extends ComputedOptions = {},
  203. M extends MethodOptions = {},
  204. Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
  205. Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
  206. Emits extends EmitsOptions = {},
  207. EmitsNames extends string = string
  208. > = ComponentOptionsBase<
  209. Props,
  210. RawBindings,
  211. D,
  212. C,
  213. M,
  214. Mixin,
  215. Extends,
  216. Emits,
  217. EmitsNames,
  218. {}
  219. > & {
  220. props?: undefined
  221. } & ThisType<
  222. CreateComponentPublicInstance<
  223. Props,
  224. RawBindings,
  225. D,
  226. C,
  227. M,
  228. Mixin,
  229. Extends,
  230. Emits
  231. >
  232. >
  233. export type WithLegacyAPI<T, D, C, M, Props> = T &
  234. Omit<Vue2ComponentOptions<Vue, D, M, C, Props>, keyof T>