-
-
Notifications
You must be signed in to change notification settings - Fork 9.2k
Expand file tree
/
Copy pathreactivity.test-d.ts
More file actions
153 lines (128 loc) · 3.6 KB
/
Copy pathreactivity.test-d.ts
File metadata and controls
153 lines (128 loc) · 3.6 KB
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
import {
type Ref,
markRaw,
reactive,
readonly,
ref,
shallowReadonly,
toRaw,
} from 'vue'
import { describe, expectType } from './utils'
describe('should support DeepReadonly', () => {
const r = readonly({ obj: { k: 'v' } })
// @ts-expect-error
r.obj = {}
// @ts-expect-error
r.obj.k = 'x'
})
// #4180
describe('readonly ref', () => {
const r = readonly(ref({ count: 1 }))
expectType<Ref>(r)
})
describe('should support markRaw', () => {
class Test<T> {
item = {} as Ref<T>
}
const test = new Test<number>()
const plain = {
ref: ref(1),
}
const r = reactive({
class: {
raw: markRaw(test),
reactive: test,
},
plain: {
raw: markRaw(plain),
reactive: plain,
},
})
expectType<Test<number>>(r.class.raw)
// @ts-expect-error it should unwrap
expectType<Test<number>>(r.class.reactive)
expectType<Ref<number>>(r.plain.raw.ref)
// @ts-expect-error it should unwrap
expectType<Ref<number>>(r.plain.reactive.ref)
})
describe('shallowReadonly ref unwrap', () => {
const r = shallowReadonly({ count: { n: ref(1) } })
// @ts-expect-error
r.count = 2
expectType<Ref>(r.count.n)
r.count.n.value = 123
})
// #3819
describe('should unwrap tuple correctly', () => {
const readonlyTuple = [ref(0)] as const
const reactiveReadonlyTuple = reactive(readonlyTuple)
expectType<Ref<number>>(reactiveReadonlyTuple[0])
const tuple: [Ref<number>] = [ref(0)]
const reactiveTuple = reactive(tuple)
expectType<Ref<number>>(reactiveTuple[0])
})
describe('should unwrap Map correctly', () => {
const map = reactive(new Map<string, Ref<number>>())
expectType<Ref<number>>(map.get('a')!)
const map2 = reactive(new Map<string, { wrap: Ref<number> }>())
expectType<number>(map2.get('a')!.wrap)
const wm = reactive(new WeakMap<object, Ref<number>>())
expectType<Ref<number>>(wm.get({})!)
const wm2 = reactive(new WeakMap<object, { wrap: Ref<number> }>())
expectType<number>(wm2.get({})!.wrap)
})
describe('should unwrap extended Map correctly', () => {
class ExtendendMap1 extends Map<string, { wrap: Ref<number> }> {
foo = ref('foo')
bar = 1
}
const emap1 = reactive(new ExtendendMap1())
expectType<string>(emap1.foo)
expectType<number>(emap1.bar)
expectType<number>(emap1.get('a')!.wrap)
})
describe('should unwrap Set correctly', () => {
const set = reactive(new Set<Ref<number>>())
expectType<Set<Ref<number>>>(set)
const set2 = reactive(new Set<{ wrap: Ref<number> }>())
expectType<Set<{ wrap: number }>>(set2)
const ws = reactive(new WeakSet<Ref<number>>())
expectType<WeakSet<Ref<number>>>(ws)
const ws2 = reactive(new WeakSet<{ wrap: Ref<number> }>())
expectType<WeakSet<{ wrap: number }>>(ws2)
})
describe('should unwrap extended Set correctly', () => {
class ExtendendSet1 extends Set<{ wrap: Ref<number> }> {
foo = ref('foo')
bar = 1
}
const eset1 = reactive(new ExtendendSet1())
expectType<string>(eset1.foo)
expectType<number>(eset1.bar)
})
describe('should not error when assignment', () => {
const arr = reactive([''])
let record: Record<number, string>
record = arr
expectType<string>(record[0])
let record2: { [key: number]: string }
record2 = arr
expectType<string>(record2[0])
})
// #7478
describe('readonly raw type', () => {
type Foo = { a: number; b: string; c: { d: number } }
const foo: Foo = {
a: 1,
b: 'b',
c: { d: 2 },
}
// readonly
const r = readonly(foo)
const rawObj = toRaw(r)
expectType<Foo>(rawObj)
// shallowReadonly
const shallowR = shallowReadonly(foo)
const shallowRawObj = toRaw(shallowR)
expectType<Foo>(shallowRawObj)
})