forked from vuejs/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathref.test-d.ts
321 lines (271 loc) · 7.23 KB
/
ref.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import {
Ref,
ref,
shallowRef,
isRef,
unref,
reactive,
expectType,
proxyRefs,
toRef,
toRefs,
ToRefs,
shallowReactive,
readonly
} from './index'
function plainType(arg: number | Ref<number>) {
// ref coercing
const coerced = ref(arg)
expectType<Ref<number>>(coerced)
// isRef as type guard
if (isRef(arg)) {
expectType<Ref<number>>(arg)
}
// ref unwrapping
expectType<number>(unref(arg))
// ref inner type should be unwrapped
const nestedRef = ref({
foo: ref(1)
})
expectType<{ foo: number }>(nestedRef.value)
// ref boolean
const falseRef = ref(false)
expectType<Ref<boolean>>(falseRef)
expectType<boolean>(falseRef.value)
// ref true
const trueRef = ref<true>(true)
expectType<Ref<true>>(trueRef)
expectType<true>(trueRef.value)
// tuple
expectType<[number, string]>(unref(ref([1, '1'])))
interface IteratorFoo {
[Symbol.iterator]: any
}
// with symbol
expectType<Ref<IteratorFoo | null | undefined>>(
ref<IteratorFoo | null | undefined>()
)
// should not unwrap ref inside arrays
const arr = ref([1, new Map<string, any>(), ref('1')]).value
const value = arr[0]
if (isRef(value)) {
expectType<Ref>(value)
} else if (typeof value === 'number') {
expectType<number>(value)
} else {
// should narrow down to Map type
// and not contain any Ref type
expectType<Map<string, any>>(value)
}
// should still unwrap in objects nested in arrays
const arr2 = ref([{ a: ref(1) }]).value
expectType<number>(arr2[0].a)
}
plainType(1)
function bailType(arg: HTMLElement | Ref<HTMLElement>) {
// ref coercing
const coerced = ref(arg)
expectType<Ref<HTMLElement>>(coerced)
// isRef as type guard
if (isRef(arg)) {
expectType<Ref<HTMLElement>>(arg)
}
// ref unwrapping
expectType<HTMLElement>(unref(arg))
// ref inner type should be unwrapped
// eslint-disable-next-line no-restricted-globals
const nestedRef = ref({ foo: ref(document.createElement('DIV')) })
expectType<Ref<{ foo: HTMLElement }>>(nestedRef)
expectType<{ foo: HTMLElement }>(nestedRef.value)
}
// eslint-disable-next-line no-restricted-globals
const el = document.createElement('DIV')
bailType(el)
function withSymbol() {
const customSymbol = Symbol()
const obj = {
[Symbol.asyncIterator]: ref(1),
[Symbol.hasInstance]: { a: ref('a') },
[Symbol.isConcatSpreadable]: { b: ref(true) },
[Symbol.iterator]: [ref(1)],
[Symbol.match]: new Set<Ref<number>>(),
[Symbol.matchAll]: new Map<number, Ref<string>>(),
[Symbol.replace]: { arr: [ref('a')] },
[Symbol.search]: { set: new Set<Ref<number>>() },
[Symbol.species]: { map: new Map<number, Ref<string>>() },
[Symbol.split]: new WeakSet<Ref<boolean>>(),
[Symbol.toPrimitive]: new WeakMap<Ref<boolean>, string>(),
[Symbol.toStringTag]: { weakSet: new WeakSet<Ref<boolean>>() },
[Symbol.unscopables]: { weakMap: new WeakMap<Ref<boolean>, string>() },
[customSymbol]: { arr: [ref(1)] }
}
const objRef = ref(obj)
expectType<Ref<number>>(objRef.value[Symbol.asyncIterator])
expectType<{ a: Ref<string> }>(objRef.value[Symbol.hasInstance])
expectType<{ b: Ref<boolean> }>(objRef.value[Symbol.isConcatSpreadable])
expectType<Ref<number>[]>(objRef.value[Symbol.iterator])
expectType<Set<Ref<number>>>(objRef.value[Symbol.match])
expectType<Map<number, Ref<string>>>(objRef.value[Symbol.matchAll])
expectType<{ arr: Ref<string>[] }>(objRef.value[Symbol.replace])
expectType<{ set: Set<Ref<number>> }>(objRef.value[Symbol.search])
expectType<{ map: Map<number, Ref<string>> }>(objRef.value[Symbol.species])
expectType<WeakSet<Ref<boolean>>>(objRef.value[Symbol.split])
expectType<WeakMap<Ref<boolean>, string>>(objRef.value[Symbol.toPrimitive])
expectType<{ weakSet: WeakSet<Ref<boolean>> }>(
objRef.value[Symbol.toStringTag]
)
expectType<{ weakMap: WeakMap<Ref<boolean>, string> }>(
objRef.value[Symbol.unscopables]
)
expectType<{ arr: Ref<number>[] }>(objRef.value[customSymbol])
}
withSymbol()
const state = reactive({
foo: {
value: 1,
label: 'bar'
}
})
expectType<string>(state.foo.label)
// shallowRef
type Status = 'initial' | 'ready' | 'invalidating'
const shallowStatus = shallowRef<Status>('initial')
if (shallowStatus.value === 'initial') {
expectType<Ref<Status>>(shallowStatus)
expectType<Status>(shallowStatus.value)
shallowStatus.value = 'invalidating'
}
const refStatus = ref<Status>('initial')
if (refStatus.value === 'initial') {
expectType<Ref<Status>>(shallowStatus)
expectType<Status>(shallowStatus.value)
refStatus.value = 'invalidating'
}
// proxyRefs: should return `reactive` directly
const r1 = reactive({
k: 'v'
})
const p1 = proxyRefs(r1)
expectType<typeof r1>(p1)
// proxyRefs: `ShallowUnwrapRef`
const r2 = {
a: ref(1),
obj: {
k: ref('foo')
}
}
const p2 = proxyRefs(r2)
expectType<number>(p2.a)
expectType<Ref<string>>(p2.obj.k)
// toRef and toRefs
{
const obj: {
a: number
b: Ref<number>
c: number | string
} = {
a: 1,
b: ref(1),
c: 1
}
// toRef
expectType<Ref<number>>(toRef(obj, 'a'))
expectType<Ref<number>>(toRef(obj, 'b'))
// Should not distribute Refs over union
expectType<Ref<number | string>>(toRef(obj, 'c'))
// toRefs
expectType<{
a: Ref<number>
b: Ref<number>
// Should not distribute Refs over union
c: Ref<number | string>
}>(toRefs(obj))
// Both should not do any unwrapping
const someReactive = shallowReactive({
a: {
b: ref(42)
}
})
const toRefResult = toRef(someReactive, 'a')
const toRefsResult = toRefs(someReactive)
expectType<Ref<number>>(toRefResult.value.b)
expectType<Ref<number>>(toRefsResult.a.value.b)
// #5188
const props = { foo: 1 } as { foo: any }
const { foo } = toRefs(props)
expectType<Ref<any>>(foo)
}
// toRef default value
{
const obj: { x?: number } = {}
const x = toRef(obj, 'x', 1)
expectType<Ref<number>>(x)
}
// readonly() + ref()
expectType<Readonly<Ref<number>>>(readonly(ref(1)))
// #2687
interface AppData {
state: 'state1' | 'state2' | 'state3'
}
const data: ToRefs<AppData> = toRefs(
reactive({
state: 'state1'
})
)
switch (data.state.value) {
case 'state1':
data.state.value = 'state2'
break
case 'state2':
data.state.value = 'state3'
break
case 'state3':
data.state.value = 'state1'
break
}
// #3954
function testUnrefGenerics<T>(p: T | Ref<T>) {
expectType<T>(unref(p))
}
testUnrefGenerics(1)
// #4771
describe('shallow reactive in reactive', () => {
const baz = reactive({
foo: shallowReactive({
a: {
b: ref(42)
}
})
})
const foo = toRef(baz, 'foo')
expectType<Ref<number>>(foo.value.a.b)
expectType<number>(foo.value.a.b.value)
})
describe('shallow ref in reactive', () => {
const x = reactive({
foo: shallowRef({
bar: {
baz: ref(123),
qux: reactive({
z: ref(123)
})
}
})
})
expectType<Ref<number>>(x.foo.bar.baz)
expectType<number>(x.foo.bar.qux.z)
})
describe('ref in shallow ref', () => {
const x = shallowRef({
a: ref(123)
})
expectType<Ref<number>>(x.value.a)
})
describe('reactive in shallow ref', () => {
const x = shallowRef({
a: reactive({
b: ref(0)
})
})
expectType<number>(x.value.a.b)
})