options.d.ts 8.9 KB

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