options.d.ts 7.9 KB

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