forked from vuejs/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreactivityMacros.test-d.ts
110 lines (92 loc) · 1.94 KB
/
reactivityMacros.test-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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { WritableComputedRef } from '@vue/reactivity'
import { expectType, ref, computed, Ref, ComputedRef } from './index'
import 'vue/macros-global'
import { RefType, RefTypes } from 'vue/macros'
// wrapping refs
// normal
let n = $(ref(1))
n = 2
// @ts-expect-error
n = 'foo'
// #4499 nullable
let msg = $(ref<string | null>(null))
msg = 'hello world'
msg = null
expectType<RefTypes.Ref | undefined>(msg![RefType])
// computed
let m = $(computed(() => n + 1))
m * 1
// @ts-expect-error
m.slice()
expectType<RefTypes.ComputedRef | undefined>(m[RefType])
// writable computed
let wc = $(
computed({
get: () => n + 1,
set: v => (n = v - 1)
})
)
wc = 2
// @ts-expect-error
wc = 'foo'
expectType<RefTypes.WritableComputedRef | undefined>(wc[RefType])
// destructure
function useFoo() {
let x = $ref(1)
let y = $computed(() => 'hi')
return $$({
x,
y,
z: 123
})
}
const fooRes = useFoo()
const { x, y, z } = $(fooRes)
expectType<number>(x)
expectType<string>(y)
expectType<number>(z)
// $ref
expectType<number>($ref(1))
expectType<number>($ref(ref(1)))
expectType<{ foo: number }>($ref({ foo: ref(1) }))
// $shallowRef
expectType<number>($shallowRef(1))
expectType<{ foo: Ref<number> }>($shallowRef({ foo: ref(1) }))
// $computed
expectType<number>($computed(() => 1))
let b = $ref(1)
expectType<number>(
$computed(() => b, {
onTrack() {}
})
)
// writable computed
expectType<number>(
$computed({
get: () => 1,
set: () => {}
})
)
// $$
const xRef = $$(x)
expectType<Ref<number>>(xRef)
const yRef = $$(y)
expectType<ComputedRef<string>>(yRef)
const c = $computed(() => 1)
const cRef = $$(c)
expectType<ComputedRef<number>>(cRef)
const c2 = $computed({
get: () => 1,
set: () => {}
})
const c2Ref = $$(c2)
expectType<WritableComputedRef<number>>(c2Ref)
// $$ on object
const obj = $$({
n,
m,
wc
})
expectType<Ref<number>>(obj.n)
expectType<ComputedRef<number>>(obj.m)
expectType<WritableComputedRef<number>>(obj.wc)