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