-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.d.ts
85 lines (77 loc) · 3.13 KB
/
types.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
type AttrTypes = string | number | RegExp | null | boolean;
type AttrParams = string[] | ObjectLike<AttrDefinition>;
type DirectiveDefinition = (inst: Component, options: DirectiveOptions, node: HTMLElement) => void | DirectiveDefObject;
type TemplateFn = (component: Component) => ComponentTemplate;
type IterateKey<T> = T extends any[] ? number : string;
type IterateValue<T> = T extends any[] ? T[number] : T[keyof T];
type PluginFn = (this: Component, ctor: ComponentConstructor, pluginOptions?: ObjectLike<any>) => void;
interface ObjectLike<T> { [key: string]: T; }
interface DirectiveDefObject {
$init?(inst: Component, options: DirectiveOptions, node: HTMLElement): void;
$inserted?(inst: Component, options: DirectiveOptions, node: HTMLElement): void;
$update(inst: Component, options: DirectiveOptions, node: HTMLElement): void;
$destroy?(inst: Component, options: DirectiveOptions, node: HTMLElement): void;
}
interface AttrDefinition {
required?: boolean;
type: string | Function;
validator?(value: any): boolean;
default?: AttrTypes | (() => AttrTypes | Object);
}
interface DirectiveOptions {
value: any;
expression: string;
modifiers: ObjectLike<boolean>;
}
interface ComponentOptions extends ComponentHooks {
model?: ObjectLike<any>;
attrs?: string[] | ObjectLike<AttrDefinition>;
filters?: ObjectLike<(...args: any[]) => any>;
children?: ObjectLike<ComponentConstructor>;
directives?: ObjectLike<DirectiveDefinition>;
}
interface ComponentTemplate {
$create(): void;
$mount(parent: string | Element, sibling?: string | boolean | Element): void;
$update(state: Component, ...args: any[]): void;
$unmount(): void;
$destroy(): void;
}
interface ComponentHooks {
willCreate?(this: Component): void;
willMount?(this: Component): void;
willUpdate?(this: Component): void;
willUnmount?(this: Component): void;
willDestroy?(this: Component): void;
didCreate?(this: Component): void;
didMount?(this: Component): void;
didUpdate?(this: Component): void;
didUnmount?(this: Component): void;
didDestroy?(this: Component): void;
}
interface Component extends ComponentTemplate {
$parent: Component;
$parentEl: HTMLElement;
$siblingEl: HTMLElement;
readonly $refs: ObjectLike<HTMLElement[]>;
readonly $slots: ObjectLike<DocumentFragment>;
readonly $filters: ObjectLike<(...args: any[]) => any>;
readonly $options: ComponentOptions;
readonly $children: Component[];
readonly $directives: ObjectLike<DirectiveDefinition>;
$get<T>(path: string): T;
$set<T>(path: string, value: T): void;
$update(): void;
$on(event: string, handler: (data?: any) => void): { $off(): void };
$once(event: string, handler: (data?: any) => void): void;
$fire(event: string, data?: any): void;
$notify(key: string): void;
$observe(key: string | string[], handler: () => void): { $unobserve(): void };
$watch(key: string, handler: (oldValue?: any, newValue?: any) => void): { $unwatch(): void };
[key: string]: any;
}
interface ComponentConstructor {
new <T extends Component>(attrs?: string[] | ObjectLike<AttrDefinition>, parent?: Component): T;
plugin(fn: PluginFn, options?: ObjectLike<any>): void;
prototype: Component;
}