Skip to content

Commit b79e4ca

Browse files
authored
types(reactivity): Simplify the SymbolExtract (#4162)
1 parent d2585e1 commit b79e4ca

File tree

3 files changed

+69
-44
lines changed

3 files changed

+69
-44
lines changed

packages/reactivity/__tests__/ref.spec.ts

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,16 +152,44 @@ describe('reactivity/ref', () => {
152152
it('should keep symbols', () => {
153153
const customSymbol = Symbol()
154154
const obj = {
155-
[Symbol.asyncIterator]: { a: 1 },
156-
[Symbol.unscopables]: { b: '1' },
157-
[customSymbol]: { c: [1, 2, 3] }
155+
[Symbol.asyncIterator]: ref(1),
156+
[Symbol.hasInstance]: { a: ref('a') },
157+
[Symbol.isConcatSpreadable]: { b: ref(true) },
158+
[Symbol.iterator]: [ref(1)],
159+
[Symbol.match]: new Set<Ref<number>>(),
160+
[Symbol.matchAll]: new Map<number, Ref<string>>(),
161+
[Symbol.replace]: { arr: [ref('a')] },
162+
[Symbol.search]: { set: new Set<Ref<number>>() },
163+
[Symbol.species]: { map: new Map<number, Ref<string>>() },
164+
[Symbol.split]: new WeakSet<Ref<boolean>>(),
165+
[Symbol.toPrimitive]: new WeakMap<Ref<boolean>, string>(),
166+
[Symbol.toStringTag]: { weakSet: new WeakSet<Ref<boolean>>() },
167+
[Symbol.unscopables]: { weakMap: new WeakMap<Ref<boolean>, string>() },
168+
[customSymbol]: { arr: [ref(1)] }
158169
}
159170

160171
const objRef = ref(obj)
161172

162-
expect(objRef.value[Symbol.asyncIterator]).toBe(obj[Symbol.asyncIterator])
163-
expect(objRef.value[Symbol.unscopables]).toBe(obj[Symbol.unscopables])
164-
expect(objRef.value[customSymbol]).toStrictEqual(obj[customSymbol])
173+
const keys: (keyof typeof obj)[] = [
174+
Symbol.asyncIterator,
175+
Symbol.hasInstance,
176+
Symbol.isConcatSpreadable,
177+
Symbol.iterator,
178+
Symbol.match,
179+
Symbol.matchAll,
180+
Symbol.replace,
181+
Symbol.search,
182+
Symbol.species,
183+
Symbol.split,
184+
Symbol.toPrimitive,
185+
Symbol.toStringTag,
186+
Symbol.unscopables,
187+
customSymbol
188+
]
189+
190+
keys.forEach(key => {
191+
expect(objRef.value[key]).toStrictEqual(obj[key])
192+
})
165193
})
166194

167195
test('unref', () => {

packages/reactivity/src/ref.ts

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -274,36 +274,7 @@ type UnwrapRefSimple<T> = T extends
274274
: T extends Array<any>
275275
? { [K in keyof T]: UnwrapRefSimple<T[K]> }
276276
: T extends object
277-
? UnwrappedObject<T>
277+
? {
278+
[P in keyof T]: P extends symbol ? T[P] : UnwrapRef<T[P]>
279+
}
278280
: T
279-
280-
// Extract all known symbols from an object
281-
// when unwrapping Object the symbols are not `in keyof`, this should cover all the
282-
// known symbols
283-
type SymbolExtract<T> = (T extends { [Symbol.asyncIterator]: infer V }
284-
? { [Symbol.asyncIterator]: V }
285-
: {}) &
286-
(T extends { [Symbol.hasInstance]: infer V }
287-
? { [Symbol.hasInstance]: V }
288-
: {}) &
289-
(T extends { [Symbol.isConcatSpreadable]: infer V }
290-
? { [Symbol.isConcatSpreadable]: V }
291-
: {}) &
292-
(T extends { [Symbol.iterator]: infer V } ? { [Symbol.iterator]: V } : {}) &
293-
(T extends { [Symbol.match]: infer V } ? { [Symbol.match]: V } : {}) &
294-
(T extends { [Symbol.matchAll]: infer V } ? { [Symbol.matchAll]: V } : {}) &
295-
(T extends { [Symbol.replace]: infer V } ? { [Symbol.replace]: V } : {}) &
296-
(T extends { [Symbol.search]: infer V } ? { [Symbol.search]: V } : {}) &
297-
(T extends { [Symbol.species]: infer V } ? { [Symbol.species]: V } : {}) &
298-
(T extends { [Symbol.split]: infer V } ? { [Symbol.split]: V } : {}) &
299-
(T extends { [Symbol.toPrimitive]: infer V }
300-
? { [Symbol.toPrimitive]: V }
301-
: {}) &
302-
(T extends { [Symbol.toStringTag]: infer V }
303-
? { [Symbol.toStringTag]: V }
304-
: {}) &
305-
(T extends { [Symbol.unscopables]: infer V }
306-
? { [Symbol.unscopables]: V }
307-
: {})
308-
309-
type UnwrappedObject<T> = { [P in keyof T]: UnwrapRef<T[P]> } & SymbolExtract<T>

test-dts/ref.test-d.ts

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,42 @@ bailType(el)
101101
function withSymbol() {
102102
const customSymbol = Symbol()
103103
const obj = {
104-
[Symbol.asyncIterator]: { a: 1 },
105-
[Symbol.unscopables]: { b: '1' },
106-
[customSymbol]: { c: [1, 2, 3] }
104+
[Symbol.asyncIterator]: ref(1),
105+
[Symbol.hasInstance]: { a: ref('a') },
106+
[Symbol.isConcatSpreadable]: { b: ref(true) },
107+
[Symbol.iterator]: [ref(1)],
108+
[Symbol.match]: new Set<Ref<number>>(),
109+
[Symbol.matchAll]: new Map<number, Ref<string>>(),
110+
[Symbol.replace]: { arr: [ref('a')] },
111+
[Symbol.search]: { set: new Set<Ref<number>>() },
112+
[Symbol.species]: { map: new Map<number, Ref<string>>() },
113+
[Symbol.split]: new WeakSet<Ref<boolean>>(),
114+
[Symbol.toPrimitive]: new WeakMap<Ref<boolean>, string>(),
115+
[Symbol.toStringTag]: { weakSet: new WeakSet<Ref<boolean>>() },
116+
[Symbol.unscopables]: { weakMap: new WeakMap<Ref<boolean>, string>() },
117+
[customSymbol]: { arr: [ref(1)] }
107118
}
108119

109120
const objRef = ref(obj)
110121

111-
expectType<{ a: number }>(objRef.value[Symbol.asyncIterator])
112-
expectType<{ b: string }>(objRef.value[Symbol.unscopables])
113-
expectType<{ c: Array<number> }>(objRef.value[customSymbol])
122+
expectType<Ref<number>>(objRef.value[Symbol.asyncIterator])
123+
expectType<{ a: Ref<string> }>(objRef.value[Symbol.hasInstance])
124+
expectType<{ b: Ref<boolean> }>(objRef.value[Symbol.isConcatSpreadable])
125+
expectType<Ref<number>[]>(objRef.value[Symbol.iterator])
126+
expectType<Set<Ref<number>>>(objRef.value[Symbol.match])
127+
expectType<Map<number, Ref<string>>>(objRef.value[Symbol.matchAll])
128+
expectType<{ arr: Ref<string>[] }>(objRef.value[Symbol.replace])
129+
expectType<{ set: Set<Ref<number>> }>(objRef.value[Symbol.search])
130+
expectType<{ map: Map<number, Ref<string>> }>(objRef.value[Symbol.species])
131+
expectType<WeakSet<Ref<boolean>>>(objRef.value[Symbol.split])
132+
expectType<WeakMap<Ref<boolean>, string>>(objRef.value[Symbol.toPrimitive])
133+
expectType<{ weakSet: WeakSet<Ref<boolean>> }>(
134+
objRef.value[Symbol.toStringTag]
135+
)
136+
expectType<{ weakMap: WeakMap<Ref<boolean>, string> }>(
137+
objRef.value[Symbol.unscopables]
138+
)
139+
expectType<{ arr: Ref<number>[] }>(objRef.value[customSymbol])
114140
}
115141

116142
withSymbol()

0 commit comments

Comments
 (0)