v3-component-options.d.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. // allow any options
  84. [key: string]: any
  85. // rewrite options api types
  86. data?: (
  87. this: CreateComponentPublicInstance<Props, {}, {}, {}, M, Mixin, Extends>,
  88. vm: CreateComponentPublicInstance<Props, {}, {}, {}, M, Mixin, Extends>
  89. ) => D
  90. computed?: C
  91. methods?: M
  92. mixins?: Mixin[]
  93. extends?: Extends
  94. emits?: (Emits | EmitNames[]) & ThisType<void>
  95. setup?: SetupFunction<
  96. Readonly<
  97. LooseRequired<
  98. Props &
  99. UnionToIntersection<ExtractOptionProp<Mixin>> &
  100. UnionToIntersection<ExtractOptionProp<Extends>>
  101. >
  102. >,
  103. RawBindings,
  104. Emits
  105. >
  106. __defaults?: Defaults
  107. }
  108. export type ComponentOptionsMixin = ComponentOptionsBase<
  109. any,
  110. any,
  111. any,
  112. any,
  113. any,
  114. any,
  115. any,
  116. any,
  117. any,
  118. any
  119. >
  120. export type ExtractComputedReturns<T extends any> = {
  121. [key in keyof T]: T[key] extends { get: (...args: any[]) => infer TReturn }
  122. ? TReturn
  123. : T[key] extends (...args: any[]) => infer TReturn
  124. ? TReturn
  125. : never
  126. }
  127. export type ComponentOptionsWithProps<
  128. PropsOptions = ComponentPropsOptions,
  129. RawBindings = Data,
  130. D = Data,
  131. C extends ComputedOptions = {},
  132. M extends MethodOptions = {},
  133. Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
  134. Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
  135. Emits extends EmitsOptions = {},
  136. EmitsNames extends string = string,
  137. Props = ExtractPropTypes<PropsOptions>,
  138. Defaults = ExtractDefaultPropTypes<PropsOptions>
  139. > = ComponentOptionsBase<
  140. Props,
  141. RawBindings,
  142. D,
  143. C,
  144. M,
  145. Mixin,
  146. Extends,
  147. Emits,
  148. EmitsNames,
  149. Defaults
  150. > & {
  151. props?: PropsOptions
  152. } & ThisType<
  153. CreateComponentPublicInstance<
  154. Props,
  155. RawBindings,
  156. D,
  157. C,
  158. M,
  159. Mixin,
  160. Extends,
  161. Emits
  162. >
  163. >
  164. export type ComponentOptionsWithArrayProps<
  165. PropNames extends string = string,
  166. RawBindings = Data,
  167. D = Data,
  168. C extends ComputedOptions = {},
  169. M extends MethodOptions = {},
  170. Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
  171. Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
  172. Emits extends EmitsOptions = {},
  173. EmitsNames extends string = string,
  174. Props = Readonly<{ [key in PropNames]?: any }>
  175. > = ComponentOptionsBase<
  176. Props,
  177. RawBindings,
  178. D,
  179. C,
  180. M,
  181. Mixin,
  182. Extends,
  183. Emits,
  184. EmitsNames,
  185. {}
  186. > & {
  187. props?: PropNames[]
  188. } & ThisType<
  189. CreateComponentPublicInstance<
  190. Props,
  191. RawBindings,
  192. D,
  193. C,
  194. M,
  195. Mixin,
  196. Extends,
  197. Emits
  198. >
  199. >
  200. export type ComponentOptionsWithoutProps<
  201. Props = {},
  202. RawBindings = Data,
  203. D = Data,
  204. C extends ComputedOptions = {},
  205. M extends MethodOptions = {},
  206. Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
  207. Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
  208. Emits extends EmitsOptions = {},
  209. EmitsNames extends string = string
  210. > = ComponentOptionsBase<
  211. Props,
  212. RawBindings,
  213. D,
  214. C,
  215. M,
  216. Mixin,
  217. Extends,
  218. Emits,
  219. EmitsNames,
  220. {}
  221. > & {
  222. props?: undefined
  223. } & ThisType<
  224. CreateComponentPublicInstance<
  225. Props,
  226. RawBindings,
  227. D,
  228. C,
  229. M,
  230. Mixin,
  231. Extends,
  232. Emits
  233. >
  234. >
  235. export type WithLegacyAPI<T, D, C, M, Props> = T &
  236. Omit<Vue2ComponentOptions<Vue, D, M, C, Props>, keyof T>