options.d.ts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. import { Vue, CreateElement, CombinedVueInstance } from './vue'
  2. import { VNode, VNodeData, VNodeDirective, NormalizedScopedSlot } from './vnode'
  3. import { SetupContext } from './v3-setup-context'
  4. import { DebuggerEvent } from './v3-generated'
  5. import { DefineComponent } from './v3-define-component'
  6. import { ComponentOptionsMixin } from './v3-component-options'
  7. type Constructor = {
  8. new (...args: any[]): any
  9. }
  10. // we don't support infer props in async component
  11. // N.B. ComponentOptions<V> is contravariant, the default generic should be bottom type
  12. export type Component<
  13. Data = DefaultData<never>,
  14. Methods = DefaultMethods<never>,
  15. Computed = DefaultComputed,
  16. Props = DefaultProps,
  17. SetupBindings = {}
  18. > =
  19. | typeof Vue
  20. | FunctionalComponentOptions<Props>
  21. | ComponentOptions<never, Data, Methods, Computed, Props, SetupBindings>
  22. | DefineComponent<any, any, any, any, any, any, any, any, any, any, any>
  23. type EsModule<T> = T | { default: T }
  24. type ImportedComponent<
  25. Data = DefaultData<never>,
  26. Methods = DefaultMethods<never>,
  27. Computed = DefaultComputed,
  28. Props = DefaultProps,
  29. SetupBindings = {}
  30. > = EsModule<Component<Data, Methods, Computed, Props, SetupBindings>>
  31. export type AsyncComponent<
  32. Data = DefaultData<never>,
  33. Methods = DefaultMethods<never>,
  34. Computed = DefaultComputed,
  35. Props = DefaultProps,
  36. SetupBindings = {}
  37. > =
  38. | AsyncComponentPromise<Data, Methods, Computed, Props, SetupBindings>
  39. | AsyncComponentFactory<Data, Methods, Computed, Props, SetupBindings>
  40. export type AsyncComponentPromise<
  41. Data = DefaultData<never>,
  42. Methods = DefaultMethods<never>,
  43. Computed = DefaultComputed,
  44. Props = DefaultProps,
  45. SetupBindings = {}
  46. > = (
  47. resolve: (
  48. component: Component<Data, Methods, Computed, Props, SetupBindings>
  49. ) => void,
  50. reject: (reason?: any) => void
  51. ) => Promise<
  52. ImportedComponent<Data, Methods, Computed, Props, SetupBindings>
  53. > | void
  54. export type AsyncComponentFactory<
  55. Data = DefaultData<never>,
  56. Methods = DefaultMethods<never>,
  57. Computed = DefaultComputed,
  58. Props = DefaultProps,
  59. SetupBindings = {}
  60. > = () => {
  61. component: Promise<
  62. ImportedComponent<Data, Methods, Computed, Props, SetupBindings>
  63. >
  64. loading?: ImportedComponent
  65. error?: ImportedComponent
  66. delay?: number
  67. timeout?: number
  68. }
  69. /**
  70. * When the `Computed` type parameter on `ComponentOptions` is inferred,
  71. * it should have a property with the return type of every get-accessor.
  72. * Since there isn't a way to query for the return type of a function, we allow TypeScript
  73. * to infer from the shape of `Accessors<Computed>` and work backwards.
  74. */
  75. export type Accessors<T> = {
  76. [K in keyof T]: (() => T[K]) | ComputedOptions<T[K]>
  77. }
  78. type DataDef<Data, Props, V> = Data | ((this: Readonly<Props> & V) => Data)
  79. /**
  80. * This type should be used when an array of strings is used for a component's `props` value.
  81. */
  82. export type ThisTypedComponentOptionsWithArrayProps<
  83. V extends Vue,
  84. Data,
  85. Methods,
  86. Computed,
  87. PropNames extends string,
  88. SetupBindings,
  89. Mixin extends ComponentOptionsMixin,
  90. Extends extends ComponentOptionsMixin
  91. > = object &
  92. ComponentOptions<
  93. V,
  94. DataDef<Data, Record<PropNames, any>, V>,
  95. Methods,
  96. Computed,
  97. PropNames[],
  98. Record<PropNames, any>,
  99. SetupBindings,
  100. Mixin,
  101. Extends
  102. > &
  103. ThisType<
  104. CombinedVueInstance<
  105. V,
  106. Data,
  107. Methods,
  108. Computed,
  109. Readonly<Record<PropNames, any>>,
  110. SetupBindings,
  111. Mixin,
  112. Extends
  113. >
  114. >
  115. /**
  116. * This type should be used when an object mapped to `PropOptions` is used for a component's `props` value.
  117. */
  118. export type ThisTypedComponentOptionsWithRecordProps<
  119. V extends Vue,
  120. Data,
  121. Methods,
  122. Computed,
  123. Props,
  124. SetupBindings,
  125. Mixin extends ComponentOptionsMixin,
  126. Extends extends ComponentOptionsMixin
  127. > = object &
  128. ComponentOptions<
  129. V,
  130. DataDef<Data, Props, V>,
  131. Methods,
  132. Computed,
  133. RecordPropsDefinition<Props>,
  134. Props,
  135. SetupBindings,
  136. Mixin,
  137. Extends
  138. > &
  139. ThisType<
  140. CombinedVueInstance<
  141. V,
  142. Data,
  143. Methods,
  144. Computed,
  145. Readonly<Props>,
  146. SetupBindings,
  147. Mixin,
  148. Extends
  149. >
  150. >
  151. type DefaultData<V> = object | ((this: V) => object)
  152. type DefaultProps = Record<string, any>
  153. type DefaultMethods<V> = { [key: string]: (this: V, ...args: any[]) => any }
  154. type DefaultComputed = { [key: string]: any }
  155. export interface ComponentOptions<
  156. V extends Vue,
  157. Data = DefaultData<V>,
  158. Methods = DefaultMethods<V>,
  159. Computed = DefaultComputed,
  160. PropsDef = PropsDefinition<DefaultProps>,
  161. Props = DefaultProps,
  162. RawBindings = {},
  163. Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
  164. Extends extends ComponentOptionsMixin = ComponentOptionsMixin
  165. > {
  166. data?: Data
  167. props?: PropsDef
  168. propsData?: object
  169. computed?: Accessors<Computed>
  170. methods?: Methods
  171. watch?: Record<string, WatchOptionsWithHandler<any> | WatchHandler<any>>
  172. setup?: (
  173. this: void,
  174. props: Props,
  175. ctx: SetupContext
  176. ) => Promise<RawBindings> | RawBindings | ((h: CreateElement) => VNode) | void
  177. el?: Element | string
  178. template?: string
  179. // hack is for functional component type inference, should not be used in user code
  180. render?(
  181. createElement: CreateElement,
  182. hack: RenderContext<Props>
  183. ): VNode | null | void
  184. renderError?(createElement: CreateElement, err: Error): VNode
  185. staticRenderFns?: ((createElement: CreateElement) => VNode)[]
  186. beforeCreate?(this: V): void
  187. created?(): void
  188. beforeDestroy?(): void
  189. destroyed?(): void
  190. beforeMount?(): void
  191. mounted?(): void
  192. beforeUpdate?(): void
  193. updated?(): void
  194. activated?(): void
  195. deactivated?(): void
  196. errorCaptured?(err: Error, vm: Vue, info: string): boolean | void
  197. serverPrefetch?(this: V): Promise<void>
  198. renderTracked?(e: DebuggerEvent): void
  199. renderTriggerd?(e: DebuggerEvent): void
  200. directives?: { [key: string]: DirectiveFunction | DirectiveOptions }
  201. components?: {
  202. [key: string]:
  203. | {}
  204. | Component<any, any, any, any, any>
  205. | AsyncComponent<any, any, any, any>
  206. }
  207. transitions?: { [key: string]: object }
  208. filters?: { [key: string]: Function }
  209. provide?: object | (() => object)
  210. inject?: InjectOptions
  211. model?: {
  212. prop?: string
  213. event?: string
  214. }
  215. parent?: Vue
  216. mixins?: (Mixin | ComponentOptions<Vue> | typeof Vue)[]
  217. name?: string
  218. // for SFC auto name inference w/ ts-loader check
  219. __name?: string
  220. // TODO: support properly inferred 'extends'
  221. extends?: Extends | ComponentOptions<Vue> | typeof Vue
  222. delimiters?: [string, string]
  223. comments?: boolean
  224. inheritAttrs?: boolean
  225. }
  226. export interface FunctionalComponentOptions<
  227. Props = DefaultProps,
  228. PropDefs = PropsDefinition<Props>
  229. > {
  230. name?: string
  231. props?: PropDefs
  232. model?: {
  233. prop?: string
  234. event?: string
  235. }
  236. inject?: InjectOptions
  237. functional: boolean
  238. render?(
  239. this: undefined,
  240. createElement: CreateElement,
  241. context: RenderContext<Props>
  242. ): VNode | VNode[]
  243. }
  244. export interface RenderContext<Props = DefaultProps> {
  245. props: Props
  246. children: VNode[]
  247. slots(): any
  248. data: VNodeData
  249. parent: Vue
  250. listeners: { [key: string]: Function | Function[] }
  251. scopedSlots: { [key: string]: NormalizedScopedSlot }
  252. injections: any
  253. }
  254. export type Prop<T> =
  255. | { (): T }
  256. | { new (...args: never[]): T & object }
  257. | { new (...args: string[]): Function }
  258. export type PropType<T> = Prop<T> | Prop<T>[]
  259. export type PropValidator<T> = PropOptions<T> | PropType<T>
  260. export interface PropOptions<T = any> {
  261. type?: PropType<T>
  262. required?: boolean
  263. default?: T | null | undefined | (() => T | null | undefined)
  264. validator?(value: unknown): boolean
  265. }
  266. export type RecordPropsDefinition<T> = {
  267. [K in keyof T]: PropValidator<T[K]>
  268. }
  269. export type ArrayPropsDefinition<T> = (keyof T)[]
  270. export type PropsDefinition<T> =
  271. | ArrayPropsDefinition<T>
  272. | RecordPropsDefinition<T>
  273. export interface ComputedOptions<T> {
  274. get?(): T
  275. set?(value: T): void
  276. cache?: boolean
  277. }
  278. export type WatchHandler<T> = string | ((val: T, oldVal: T) => void)
  279. export interface WatchOptions {
  280. deep?: boolean
  281. immediate?: boolean
  282. }
  283. export interface WatchOptionsWithHandler<T> extends WatchOptions {
  284. handler: WatchHandler<T>
  285. }
  286. export interface DirectiveBinding extends Readonly<VNodeDirective> {
  287. readonly modifiers: { [key: string]: boolean }
  288. }
  289. export type DirectiveFunction = (
  290. el: HTMLElement,
  291. binding: DirectiveBinding,
  292. vnode: VNode,
  293. oldVnode: VNode
  294. ) => void
  295. export interface DirectiveOptions {
  296. bind?: DirectiveFunction
  297. inserted?: DirectiveFunction
  298. update?: DirectiveFunction
  299. componentUpdated?: DirectiveFunction
  300. unbind?: DirectiveFunction
  301. }
  302. export type InjectKey = string | symbol
  303. export type InjectOptions =
  304. | {
  305. [key: string]: InjectKey | { from?: InjectKey; default?: any }
  306. }
  307. | string[]