/
vueLikeDataAndPropsInference2.js
64 lines (54 loc) · 1.27 KB
/
vueLikeDataAndPropsInference2.js
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
//// [vueLikeDataAndPropsInference2.ts]
interface Instance {
_instanceBrand: never
}
type DataDef<Data, Props> = (this: Readonly<Props> & Instance) => Data
type PropsDefinition<T> = {
[K in keyof T]: T[K]
}
interface Options<
Data = object | ((this: Instance) => object),
PropsDef = PropsDefinition<Record<string, any>>
> {
data?: Data
props?: PropsDef
watch?: Record<string, WatchHandler<any>>
}
type WatchHandler<T> = (val: T, oldVal: T) => void;
type ThisTypedOptions<Data, Props> =
object &
Options<DataDef<Data, Props>, PropsDefinition<Props>> &
ThisType<Data & Readonly<Props> & Instance>
declare function test<Data, Props>(fn: ThisTypedOptions<Data, Props>): void;
declare function test(fn: Options): void;
test({
props: {
foo: ''
},
data(): { bar: boolean } {
return {
bar: true
}
},
watch: {
foo(newVal: string, oldVal: string): void {
this.bar = false
}
}
})
//// [vueLikeDataAndPropsInference2.js]
test({
props: {
foo: ''
},
data: function () {
return {
bar: true
};
},
watch: {
foo: function (newVal, oldVal) {
this.bar = false;
}
}
});