options.d.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. import { Vue, CreateElement, CombinedVueInstance } from "./vue";
  2. import { VNode, VNodeData, VNodeDirective } from "./vnode";
  3. type Constructor = {
  4. new (...args: any[]): any;
  5. }
  6. // we don't support infer props in async component
  7. // N.B. ComponentOptions<V> is contravariant, the default generic should be bottom type
  8. export type Component<Data=DefaultData<never>, Methods=DefaultMethods<never>, Computed=DefaultComputed, Props=DefaultProps> =
  9. | typeof Vue
  10. | FunctionalComponentOptions<Props>
  11. | ComponentOptions<never, Data, Methods, Computed, Props>
  12. interface EsModuleComponent {
  13. default: Component
  14. }
  15. export type AsyncComponent<Data=DefaultData<never>, Methods=DefaultMethods<never>, Computed=DefaultComputed, Props=DefaultProps>
  16. = AsyncComponentPromise<Data, Methods, Computed, Props>
  17. | AsyncComponentFactory<Data, Methods, Computed, Props>
  18. export type AsyncComponentPromise<Data=DefaultData<never>, Methods=DefaultMethods<never>, Computed=DefaultComputed, Props=DefaultProps> = (
  19. resolve: (component: Component<Data, Methods, Computed, Props>) => void,
  20. reject: (reason?: any) => void
  21. ) => Promise<Component | EsModuleComponent> | void;
  22. export type AsyncComponentFactory<Data=DefaultData<never>, Methods=DefaultMethods<never>, Computed=DefaultComputed, Props=DefaultProps> = () => {
  23. component: AsyncComponentPromise<Data, Methods, Computed, Props>;
  24. loading?: Component | EsModuleComponent;
  25. error?: Component | EsModuleComponent;
  26. delay?: number;
  27. timeout?: number;
  28. }
  29. /**
  30. * When the `Computed` type parameter on `ComponentOptions` is inferred,
  31. * it should have a property with the return type of every get-accessor.
  32. * Since there isn't a way to query for the return type of a function, we allow TypeScript
  33. * to infer from the shape of `Accessors<Computed>` and work backwards.
  34. */
  35. export type Accessors<T> = {
  36. [K in keyof T]: (() => T[K]) | ComputedOptions<T[K]>
  37. }
  38. type DataDef<Data, Props, V> = Data | ((this: Readonly<Props> & V) => Data)
  39. /**
  40. * This type should be used when an array of strings is used for a component's `props` value.
  41. */
  42. export type ThisTypedComponentOptionsWithArrayProps<V extends Vue, Data, Methods, Computed, PropNames extends string> =
  43. object &
  44. ComponentOptions<V, DataDef<Data, Record<PropNames, any>, V>, Methods, Computed, PropNames[], Record<PropNames, any>> &
  45. ThisType<CombinedVueInstance<V, Data, Methods, Computed, Readonly<Record<PropNames, any>>>>;
  46. /**
  47. * This type should be used when an object mapped to `PropOptions` is used for a component's `props` value.
  48. */
  49. export type ThisTypedComponentOptionsWithRecordProps<V extends Vue, Data, Methods, Computed, Props> =
  50. object &
  51. ComponentOptions<V, DataDef<Data, Props, V>, Methods, Computed, RecordPropsDefinition<Props>, Props> &
  52. ThisType<CombinedVueInstance<V, Data, Methods, Computed, Readonly<Props>>>;
  53. type DefaultData<V> = object | ((this: V) => object);
  54. type DefaultProps = Record<string, any>;
  55. type DefaultMethods<V> = { [key: string]: (this: V, ...args: any[]) => any };
  56. type DefaultComputed = { [key: string]: any };
  57. export interface ComponentOptions<
  58. V extends Vue,
  59. Data=DefaultData<V>,
  60. Methods=DefaultMethods<V>,
  61. Computed=DefaultComputed,
  62. PropsDef=PropsDefinition<DefaultProps>,
  63. Props=DefaultProps> {
  64. data?: Data;
  65. props?: PropsDef;
  66. propsData?: object;
  67. computed?: Accessors<Computed>;
  68. methods?: Methods;
  69. watch?: Record<string, WatchOptionsWithHandler<any> | WatchHandler<any> | string>;
  70. el?: Element | string;
  71. template?: string;
  72. // hack is for functional component type inference, should not be used in user code
  73. render?(createElement: CreateElement, hack: RenderContext<Props>): VNode;
  74. renderError?(createElement: CreateElement, err: Error): VNode;
  75. staticRenderFns?: ((createElement: CreateElement) => VNode)[];
  76. beforeCreate?(this: V): void;
  77. created?(): void;
  78. beforeDestroy?(): void;
  79. destroyed?(): void;
  80. beforeMount?(): void;
  81. mounted?(): void;
  82. beforeUpdate?(): void;
  83. updated?(): void;
  84. activated?(): void;
  85. deactivated?(): void;
  86. errorCaptured?(err: Error, vm: Vue, info: string): boolean | void;
  87. directives?: { [key: string]: DirectiveFunction | DirectiveOptions };
  88. components?: { [key: string]: Component<any, any, any, any> | AsyncComponent<any, any, any, any> };
  89. transitions?: { [key: string]: object };
  90. filters?: { [key: string]: Function };
  91. provide?: object | (() => object);
  92. inject?: InjectOptions;
  93. model?: {
  94. prop?: string;
  95. event?: string;
  96. };
  97. parent?: Vue;
  98. mixins?: (ComponentOptions<Vue> | typeof Vue)[];
  99. name?: string;
  100. // TODO: support properly inferred 'extends'
  101. extends?: ComponentOptions<Vue> | typeof Vue;
  102. delimiters?: [string, string];
  103. comments?: boolean;
  104. inheritAttrs?: boolean;
  105. }
  106. export interface FunctionalComponentOptions<Props = DefaultProps, PropDefs = PropsDefinition<Props>> {
  107. name?: string;
  108. props?: PropDefs;
  109. model?: {
  110. prop?: string;
  111. event?: string;
  112. };
  113. inject?: InjectOptions;
  114. functional: boolean;
  115. render?(this: undefined, createElement: CreateElement, context: RenderContext<Props>): VNode | VNode[];
  116. }
  117. export interface RenderContext<Props=DefaultProps> {
  118. props: Props;
  119. children: VNode[];
  120. slots(): any;
  121. data: VNodeData;
  122. parent: Vue;
  123. listeners: { [key: string]: Function | Function[] };
  124. injections: any
  125. }
  126. export type Prop<T> = { (): T } | { new (...args: any[]): T & object }
  127. export type PropValidator<T> = PropOptions<T> | Prop<T> | Prop<T>[];
  128. export interface PropOptions<T=any> {
  129. type?: Prop<T> | Prop<T>[];
  130. required?: boolean;
  131. default?: T | null | undefined | (() => T | null | undefined);
  132. validator?(value: T): boolean;
  133. }
  134. export type RecordPropsDefinition<T> = {
  135. [K in keyof T]: PropValidator<T[K]>
  136. }
  137. export type ArrayPropsDefinition<T> = (keyof T)[];
  138. export type PropsDefinition<T> = ArrayPropsDefinition<T> | RecordPropsDefinition<T>;
  139. export interface ComputedOptions<T> {
  140. get?(): T;
  141. set?(value: T): void;
  142. cache?: boolean;
  143. }
  144. export type WatchHandler<T> = (val: T, oldVal: T) => void;
  145. export interface WatchOptions {
  146. deep?: boolean;
  147. immediate?: boolean;
  148. }
  149. export interface WatchOptionsWithHandler<T> extends WatchOptions {
  150. handler: WatchHandler<T>;
  151. }
  152. export interface DirectiveBinding extends Readonly<VNodeDirective> {
  153. readonly modifiers: { [key: string]: boolean };
  154. }
  155. export type DirectiveFunction = (
  156. el: HTMLElement,
  157. binding: DirectiveBinding,
  158. vnode: VNode,
  159. oldVnode: VNode
  160. ) => void;
  161. export interface DirectiveOptions {
  162. bind?: DirectiveFunction;
  163. inserted?: DirectiveFunction;
  164. update?: DirectiveFunction;
  165. componentUpdated?: DirectiveFunction;
  166. unbind?: DirectiveFunction;
  167. }
  168. export type InjectKey = string | symbol;
  169. export type InjectOptions = {
  170. [key: string]: InjectKey | { from?: InjectKey, default?: any }
  171. } | string[];