UNPKG

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