2017-10-06 18:45:14 +00:00
|
|
|
import { Vue, CreateElement, CombinedVueInstance } from "./vue";
|
2019-03-05 21:46:56 +00:00
|
|
|
import { VNode, VNodeData, VNodeDirective, NormalizedScopedSlot } from "./vnode";
|
2016-08-23 19:01:30 +00:00
|
|
|
|
|
|
|
type Constructor = {
|
|
|
|
new (...args: any[]): any;
|
|
|
|
}
|
|
|
|
|
2017-10-06 18:45:14 +00:00
|
|
|
// we don't support infer props in async component
|
2018-01-03 06:33:48 +00:00
|
|
|
// N.B. ComponentOptions<V> is contravariant, the default generic should be bottom type
|
|
|
|
export type Component<Data=DefaultData<never>, Methods=DefaultMethods<never>, Computed=DefaultComputed, Props=DefaultProps> =
|
2017-10-06 18:45:14 +00:00
|
|
|
| typeof Vue
|
|
|
|
| FunctionalComponentOptions<Props>
|
2018-01-03 06:33:48 +00:00
|
|
|
| ComponentOptions<never, Data, Methods, Computed, Props>
|
2017-09-05 19:25:16 +00:00
|
|
|
|
|
|
|
interface EsModuleComponent {
|
|
|
|
default: Component
|
|
|
|
}
|
2019-01-18 21:43:17 +00:00
|
|
|
|
2018-12-05 23:00:56 +00:00
|
|
|
export type AsyncComponent<Data=DefaultData<never>, Methods=DefaultMethods<never>, Computed=DefaultComputed, Props=DefaultProps>
|
|
|
|
= AsyncComponentPromise<Data, Methods, Computed, Props>
|
|
|
|
| AsyncComponentFactory<Data, Methods, Computed, Props>
|
2017-09-05 19:25:16 +00:00
|
|
|
|
2018-12-05 23:00:56 +00:00
|
|
|
export type AsyncComponentPromise<Data=DefaultData<never>, Methods=DefaultMethods<never>, Computed=DefaultComputed, Props=DefaultProps> = (
|
2017-10-06 18:45:14 +00:00
|
|
|
resolve: (component: Component<Data, Methods, Computed, Props>) => void,
|
2016-09-30 17:57:38 +00:00
|
|
|
reject: (reason?: any) => void
|
2017-10-06 18:45:14 +00:00
|
|
|
) => Promise<Component | EsModuleComponent> | void;
|
|
|
|
|
2018-12-05 23:00:56 +00:00
|
|
|
export type AsyncComponentFactory<Data=DefaultData<never>, Methods=DefaultMethods<never>, Computed=DefaultComputed, Props=DefaultProps> = () => {
|
|
|
|
component: AsyncComponentPromise<Data, Methods, Computed, Props>;
|
|
|
|
loading?: Component | EsModuleComponent;
|
|
|
|
error?: Component | EsModuleComponent;
|
|
|
|
delay?: number;
|
|
|
|
timeout?: number;
|
|
|
|
}
|
|
|
|
|
2017-10-06 18:45:14 +00:00
|
|
|
/**
|
|
|
|
* When the `Computed` type parameter on `ComponentOptions` is inferred,
|
|
|
|
* it should have a property with the return type of every get-accessor.
|
|
|
|
* Since there isn't a way to query for the return type of a function, we allow TypeScript
|
|
|
|
* to infer from the shape of `Accessors<Computed>` and work backwards.
|
|
|
|
*/
|
|
|
|
export type Accessors<T> = {
|
|
|
|
[K in keyof T]: (() => T[K]) | ComputedOptions<T[K]>
|
|
|
|
}
|
2016-09-30 17:57:38 +00:00
|
|
|
|
2018-03-08 18:55:52 +00:00
|
|
|
type DataDef<Data, Props, V> = Data | ((this: Readonly<Props> & V) => Data)
|
2017-10-06 18:45:14 +00:00
|
|
|
/**
|
|
|
|
* This type should be used when an array of strings is used for a component's `props` value.
|
|
|
|
*/
|
|
|
|
export type ThisTypedComponentOptionsWithArrayProps<V extends Vue, Data, Methods, Computed, PropNames extends string> =
|
|
|
|
object &
|
2018-03-08 18:55:52 +00:00
|
|
|
ComponentOptions<V, DataDef<Data, Record<PropNames, any>, V>, Methods, Computed, PropNames[], Record<PropNames, any>> &
|
2017-10-06 18:45:14 +00:00
|
|
|
ThisType<CombinedVueInstance<V, Data, Methods, Computed, Readonly<Record<PropNames, any>>>>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This type should be used when an object mapped to `PropOptions` is used for a component's `props` value.
|
|
|
|
*/
|
|
|
|
export type ThisTypedComponentOptionsWithRecordProps<V extends Vue, Data, Methods, Computed, Props> =
|
|
|
|
object &
|
2018-03-08 18:55:52 +00:00
|
|
|
ComponentOptions<V, DataDef<Data, Props, V>, Methods, Computed, RecordPropsDefinition<Props>, Props> &
|
2017-10-06 18:45:14 +00:00
|
|
|
ThisType<CombinedVueInstance<V, Data, Methods, Computed, Readonly<Props>>>;
|
|
|
|
|
|
|
|
type DefaultData<V> = object | ((this: V) => object);
|
|
|
|
type DefaultProps = Record<string, any>;
|
|
|
|
type DefaultMethods<V> = { [key: string]: (this: V, ...args: any[]) => any };
|
|
|
|
type DefaultComputed = { [key: string]: any };
|
|
|
|
export interface ComponentOptions<
|
|
|
|
V extends Vue,
|
|
|
|
Data=DefaultData<V>,
|
|
|
|
Methods=DefaultMethods<V>,
|
|
|
|
Computed=DefaultComputed,
|
2018-03-08 18:55:52 +00:00
|
|
|
PropsDef=PropsDefinition<DefaultProps>,
|
|
|
|
Props=DefaultProps> {
|
2017-10-06 18:45:14 +00:00
|
|
|
data?: Data;
|
|
|
|
props?: PropsDef;
|
2017-11-27 14:30:58 +00:00
|
|
|
propsData?: object;
|
2017-10-06 18:45:14 +00:00
|
|
|
computed?: Accessors<Computed>;
|
|
|
|
methods?: Methods;
|
2020-09-21 14:13:21 +00:00
|
|
|
watch?: Record<string, WatchOptionsWithHandler<any> | WatchHandler<any>>;
|
2016-08-23 19:01:30 +00:00
|
|
|
|
2017-11-27 14:30:58 +00:00
|
|
|
el?: Element | string;
|
2016-08-23 19:01:30 +00:00
|
|
|
template?: string;
|
2018-12-01 04:58:58 +00:00
|
|
|
// hack is for functional component type inference, should not be used in user code
|
2018-03-08 18:55:52 +00:00
|
|
|
render?(createElement: CreateElement, hack: RenderContext<Props>): VNode;
|
2018-12-01 04:28:29 +00:00
|
|
|
renderError?(createElement: CreateElement, err: Error): VNode;
|
2016-10-08 18:15:36 +00:00
|
|
|
staticRenderFns?: ((createElement: CreateElement) => VNode)[];
|
2016-08-23 19:01:30 +00:00
|
|
|
|
2016-09-13 23:59:14 +00:00
|
|
|
beforeCreate?(this: V): void;
|
2017-10-06 18:45:14 +00:00
|
|
|
created?(): void;
|
|
|
|
beforeDestroy?(): void;
|
|
|
|
destroyed?(): void;
|
|
|
|
beforeMount?(): void;
|
|
|
|
mounted?(): void;
|
|
|
|
beforeUpdate?(): void;
|
|
|
|
updated?(): void;
|
|
|
|
activated?(): void;
|
|
|
|
deactivated?(): void;
|
2018-03-08 17:05:03 +00:00
|
|
|
errorCaptured?(err: Error, vm: Vue, info: string): boolean | void;
|
2019-01-18 21:43:17 +00:00
|
|
|
serverPrefetch?(this: V): Promise<void>;
|
2017-10-06 18:45:14 +00:00
|
|
|
|
|
|
|
directives?: { [key: string]: DirectiveFunction | DirectiveOptions };
|
|
|
|
components?: { [key: string]: Component<any, any, any, any> | AsyncComponent<any, any, any, any> };
|
2017-11-27 14:30:58 +00:00
|
|
|
transitions?: { [key: string]: object };
|
2016-08-23 19:01:30 +00:00
|
|
|
filters?: { [key: string]: Function };
|
|
|
|
|
2017-11-27 14:30:58 +00:00
|
|
|
provide?: object | (() => object);
|
2017-10-06 21:41:54 +00:00
|
|
|
inject?: InjectOptions;
|
2017-02-22 03:35:50 +00:00
|
|
|
|
|
|
|
model?: {
|
|
|
|
prop?: string;
|
|
|
|
event?: string;
|
|
|
|
};
|
|
|
|
|
2016-08-23 19:01:30 +00:00
|
|
|
parent?: Vue;
|
2016-09-13 23:59:14 +00:00
|
|
|
mixins?: (ComponentOptions<Vue> | typeof Vue)[];
|
2016-08-23 19:01:30 +00:00
|
|
|
name?: string;
|
2017-10-06 18:45:14 +00:00
|
|
|
// TODO: support properly inferred 'extends'
|
2016-09-13 23:59:14 +00:00
|
|
|
extends?: ComponentOptions<Vue> | typeof Vue;
|
2016-08-23 19:01:30 +00:00
|
|
|
delimiters?: [string, string];
|
2017-06-30 00:56:23 +00:00
|
|
|
comments?: boolean;
|
2017-07-11 14:43:31 +00:00
|
|
|
inheritAttrs?: boolean;
|
2016-08-23 19:01:30 +00:00
|
|
|
}
|
|
|
|
|
2017-10-06 18:45:14 +00:00
|
|
|
export interface FunctionalComponentOptions<Props = DefaultProps, PropDefs = PropsDefinition<Props>> {
|
2017-06-13 09:49:52 +00:00
|
|
|
name?: string;
|
2017-10-06 18:45:14 +00:00
|
|
|
props?: PropDefs;
|
2018-10-24 17:03:48 +00:00
|
|
|
model?: {
|
|
|
|
prop?: string;
|
|
|
|
event?: string;
|
|
|
|
};
|
2017-10-06 21:41:54 +00:00
|
|
|
inject?: InjectOptions;
|
2016-09-05 10:53:34 +00:00
|
|
|
functional: boolean;
|
2018-10-24 17:03:29 +00:00
|
|
|
render?(this: undefined, createElement: CreateElement, context: RenderContext<Props>): VNode | VNode[];
|
2016-09-05 10:53:34 +00:00
|
|
|
}
|
|
|
|
|
2017-10-06 18:45:14 +00:00
|
|
|
export interface RenderContext<Props=DefaultProps> {
|
|
|
|
props: Props;
|
2016-09-05 10:53:34 +00:00
|
|
|
children: VNode[];
|
2016-11-03 17:07:07 +00:00
|
|
|
slots(): any;
|
2016-09-05 10:53:34 +00:00
|
|
|
data: VNodeData;
|
|
|
|
parent: Vue;
|
2018-03-08 16:52:01 +00:00
|
|
|
listeners: { [key: string]: Function | Function[] };
|
2019-03-05 21:46:56 +00:00
|
|
|
scopedSlots: { [key: string]: NormalizedScopedSlot };
|
2017-04-05 06:36:15 +00:00
|
|
|
injections: any
|
2016-09-05 10:53:34 +00:00
|
|
|
}
|
|
|
|
|
2019-10-30 14:45:29 +00:00
|
|
|
export type Prop<T> = { (): T } | { new(...args: never[]): T & object } | { new(...args: string[]): Function }
|
2017-10-06 18:45:14 +00:00
|
|
|
|
2018-12-05 22:50:13 +00:00
|
|
|
export type PropType<T> = Prop<T> | Prop<T>[];
|
|
|
|
|
|
|
|
export type PropValidator<T> = PropOptions<T> | PropType<T>;
|
2017-10-06 18:45:14 +00:00
|
|
|
|
|
|
|
export interface PropOptions<T=any> {
|
2018-12-05 22:50:13 +00:00
|
|
|
type?: PropType<T>;
|
2016-08-23 19:01:30 +00:00
|
|
|
required?: boolean;
|
2018-09-18 02:33:58 +00:00
|
|
|
default?: T | null | undefined | (() => T | null | undefined);
|
2017-10-06 18:45:14 +00:00
|
|
|
validator?(value: T): boolean;
|
2016-08-23 19:01:30 +00:00
|
|
|
}
|
|
|
|
|
2017-10-06 18:45:14 +00:00
|
|
|
export type RecordPropsDefinition<T> = {
|
|
|
|
[K in keyof T]: PropValidator<T[K]>
|
|
|
|
}
|
|
|
|
export type ArrayPropsDefinition<T> = (keyof T)[];
|
|
|
|
export type PropsDefinition<T> = ArrayPropsDefinition<T> | RecordPropsDefinition<T>;
|
|
|
|
|
|
|
|
export interface ComputedOptions<T> {
|
|
|
|
get?(): T;
|
|
|
|
set?(value: T): void;
|
2016-08-23 19:01:30 +00:00
|
|
|
cache?: boolean;
|
|
|
|
}
|
|
|
|
|
2020-09-21 14:13:21 +00:00
|
|
|
export type WatchHandler<T> = string | ((val: T, oldVal: T) => void);
|
2016-08-23 19:01:30 +00:00
|
|
|
|
|
|
|
export interface WatchOptions {
|
|
|
|
deep?: boolean;
|
|
|
|
immediate?: boolean;
|
|
|
|
}
|
|
|
|
|
2017-10-06 18:45:14 +00:00
|
|
|
export interface WatchOptionsWithHandler<T> extends WatchOptions {
|
|
|
|
handler: WatchHandler<T>;
|
|
|
|
}
|
|
|
|
|
2018-10-22 20:09:23 +00:00
|
|
|
export interface DirectiveBinding extends Readonly<VNodeDirective> {
|
|
|
|
readonly modifiers: { [key: string]: boolean };
|
|
|
|
}
|
|
|
|
|
2016-08-23 19:01:30 +00:00
|
|
|
export type DirectiveFunction = (
|
|
|
|
el: HTMLElement,
|
2018-10-22 20:09:23 +00:00
|
|
|
binding: DirectiveBinding,
|
2016-08-23 19:01:30 +00:00
|
|
|
vnode: VNode,
|
|
|
|
oldVnode: VNode
|
|
|
|
) => void;
|
|
|
|
|
|
|
|
export interface DirectiveOptions {
|
|
|
|
bind?: DirectiveFunction;
|
2016-09-16 16:40:13 +00:00
|
|
|
inserted?: DirectiveFunction;
|
2016-08-23 19:01:30 +00:00
|
|
|
update?: DirectiveFunction;
|
|
|
|
componentUpdated?: DirectiveFunction;
|
|
|
|
unbind?: DirectiveFunction;
|
|
|
|
}
|
2017-10-06 21:41:54 +00:00
|
|
|
|
|
|
|
export type InjectKey = string | symbol;
|
|
|
|
|
|
|
|
export type InjectOptions = {
|
|
|
|
[key: string]: InjectKey | { from?: InjectKey, default?: any }
|
|
|
|
} | string[];
|