options.d.ts 8.1 KB

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