options.d.ts 5.6 KB

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