options.d.ts 5.5 KB

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