forked from vuejs/composition-api
/
watch.test-d.tsx
90 lines (80 loc) · 2.26 KB
/
watch.test-d.tsx
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
86
87
88
89
90
import { ref, computed, watch, expectType } from './index'
const source = ref('foo')
const source2 = computed(() => source.value)
const source3 = () => 1
// lazy watcher will have consistent types for oldValue.
watch(source, (value, oldValue) => {
expectType<string>(value)
expectType<string>(oldValue)
})
// spread array
watch(
[source, source2, source3],
([source1, source2, source3], [oldSource1, oldSource2, oldSource3]) => {
expectType<string>(source1)
expectType<string>(source2)
expectType<number>(source3)
expectType<string>(oldSource1)
expectType<string>(oldSource2)
expectType<number>(oldSource3)
}
)
// const array
watch([source, source2, source3] as const, (values, oldValues) => {
expectType<Readonly<[string, string, number]>>(values)
expectType<Readonly<[string, string, number]>>(oldValues)
expectType<string>(values[0])
expectType<string>(values[1])
expectType<number>(values[2])
expectType<string>(oldValues[0])
expectType<string>(oldValues[1])
expectType<number>(oldValues[2])
})
// const spread array
watch(
[source, source2, source3] as const,
([source1, source2, source3], [oldSource1, oldSource2, oldSource3]) => {
expectType<string>(source1)
expectType<string>(source2)
expectType<number>(source3)
expectType<string>(oldSource1)
expectType<string>(oldSource2)
expectType<number>(oldSource3)
}
)
// immediate watcher's oldValue will be undefined on first run.
watch(
source,
(value, oldValue) => {
expectType<string>(value)
expectType<string | undefined>(oldValue)
},
{ immediate: true }
)
watch(
[source, source2, source3],
(values, oldValues) => {
expectType<(string | number)[]>(values)
expectType<(string | number | undefined)[]>(oldValues)
},
{ immediate: true }
)
// const array
watch(
[source, source2, source3] as const,
(values, oldValues) => {
expectType<Readonly<[string, string, number]>>(values)
expectType<
Readonly<[string | undefined, string | undefined, number | undefined]>
>(oldValues)
},
{ immediate: true }
)
// should provide correct ref.value inner type to callbacks
const nestedRefSource = ref({
foo: ref(1),
})
watch(nestedRefSource, (v, ov) => {
expectType<{ foo: number }>(v)
expectType<{ foo: number }>(ov)
})