Skip to content

Commit 21dfe96

Browse files
authored
types: Simplify the SymbolExtract (#774)
Co-authored-by: webfansplz <>
1 parent 0dfcdb6 commit 21dfe96

File tree

3 files changed

+69
-43
lines changed

3 files changed

+69
-43
lines changed

src/reactivity/ref.ts

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -34,39 +34,11 @@ type UnwrapRefSimple<T> = T extends Function | CollectionTypes | BaseTypes | Ref
3434
: T extends Array<any>
3535
? { [K in keyof T]: UnwrapRefSimple<T[K]> }
3636
: T extends object
37-
? UnwrappedObject<T>
37+
? {
38+
[P in keyof T]: P extends symbol ? T[P] : UnwrapRef<T[P]>
39+
}
3840
: T
3941

40-
// Extract all known symbols from an object
41-
// when unwrapping Object the symbols are not `in keyof`, this should cover all the
42-
// known symbols
43-
type SymbolExtract<T> = (T extends { [Symbol.asyncIterator]: infer V }
44-
? { [Symbol.asyncIterator]: V }
45-
: {}) &
46-
(T extends { [Symbol.hasInstance]: infer V }
47-
? { [Symbol.hasInstance]: V }
48-
: {}) &
49-
(T extends { [Symbol.isConcatSpreadable]: infer V }
50-
? { [Symbol.isConcatSpreadable]: V }
51-
: {}) &
52-
(T extends { [Symbol.iterator]: infer V } ? { [Symbol.iterator]: V } : {}) &
53-
(T extends { [Symbol.match]: infer V } ? { [Symbol.match]: V } : {}) &
54-
(T extends { [Symbol.replace]: infer V } ? { [Symbol.replace]: V } : {}) &
55-
(T extends { [Symbol.search]: infer V } ? { [Symbol.search]: V } : {}) &
56-
(T extends { [Symbol.species]: infer V } ? { [Symbol.species]: V } : {}) &
57-
(T extends { [Symbol.split]: infer V } ? { [Symbol.split]: V } : {}) &
58-
(T extends { [Symbol.toPrimitive]: infer V }
59-
? { [Symbol.toPrimitive]: V }
60-
: {}) &
61-
(T extends { [Symbol.toStringTag]: infer V }
62-
? { [Symbol.toStringTag]: V }
63-
: {}) &
64-
(T extends { [Symbol.unscopables]: infer V }
65-
? { [Symbol.unscopables]: V }
66-
: {})
67-
68-
type UnwrappedObject<T> = { [P in keyof T]: UnwrapRef<T[P]> } & SymbolExtract<T>
69-
7042
interface RefOption<T> {
7143
get(): T
7244
set?(x: T): void

test-dts/ref.test-d.ts

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,42 @@ bailType(el)
7878
function withSymbol() {
7979
const customSymbol = Symbol()
8080
const obj = {
81-
[Symbol.asyncIterator]: { a: 1 },
82-
[Symbol.unscopables]: { b: '1' },
83-
[customSymbol]: { c: [1, 2, 3] },
81+
[Symbol.asyncIterator]: ref(1),
82+
[Symbol.hasInstance]: { a: ref('a') },
83+
[Symbol.isConcatSpreadable]: { b: ref(true) },
84+
[Symbol.iterator]: [ref(1)],
85+
[Symbol.match]: new Set<Ref<number>>(),
86+
[Symbol.matchAll]: new Map<number, Ref<string>>(),
87+
[Symbol.replace]: { arr: [ref('a')] },
88+
[Symbol.search]: { set: new Set<Ref<number>>() },
89+
[Symbol.species]: { map: new Map<number, Ref<string>>() },
90+
[Symbol.split]: new WeakSet<Ref<boolean>>(),
91+
[Symbol.toPrimitive]: new WeakMap<Ref<boolean>, string>(),
92+
[Symbol.toStringTag]: { weakSet: new WeakSet<Ref<boolean>>() },
93+
[Symbol.unscopables]: { weakMap: new WeakMap<Ref<boolean>, string>() },
94+
[customSymbol]: { arr: [ref(1)] },
8495
}
8596

8697
const objRef = ref(obj)
8798

88-
expectType<{ a: number }>(objRef.value[Symbol.asyncIterator])
89-
expectType<{ b: string }>(objRef.value[Symbol.unscopables])
90-
expectType<{ c: Array<number> }>(objRef.value[customSymbol])
99+
expectType<Ref<number>>(objRef.value[Symbol.asyncIterator])
100+
expectType<{ a: Ref<string> }>(objRef.value[Symbol.hasInstance])
101+
expectType<{ b: Ref<boolean> }>(objRef.value[Symbol.isConcatSpreadable])
102+
expectType<Ref<number>[]>(objRef.value[Symbol.iterator])
103+
expectType<Set<Ref<number>>>(objRef.value[Symbol.match])
104+
expectType<Map<number, Ref<string>>>(objRef.value[Symbol.matchAll])
105+
expectType<{ arr: Ref<string>[] }>(objRef.value[Symbol.replace])
106+
expectType<{ set: Set<Ref<number>> }>(objRef.value[Symbol.search])
107+
expectType<{ map: Map<number, Ref<string>> }>(objRef.value[Symbol.species])
108+
expectType<WeakSet<Ref<boolean>>>(objRef.value[Symbol.split])
109+
expectType<WeakMap<Ref<boolean>, string>>(objRef.value[Symbol.toPrimitive])
110+
expectType<{ weakSet: WeakSet<Ref<boolean>> }>(
111+
objRef.value[Symbol.toStringTag]
112+
)
113+
expectType<{ weakMap: WeakMap<Ref<boolean>, string> }>(
114+
objRef.value[Symbol.unscopables]
115+
)
116+
expectType<{ arr: Ref<number>[] }>(objRef.value[customSymbol])
91117
}
92118

93119
withSymbol()

test/v3/reactivity/ref.spec.ts

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,16 +167,44 @@ describe('reactivity/ref', () => {
167167
it('should keep symbols', () => {
168168
const customSymbol = Symbol()
169169
const obj = {
170-
[Symbol.asyncIterator]: { a: 1 },
171-
[Symbol.unscopables]: { b: '1' },
172-
[customSymbol]: { c: [1, 2, 3] },
170+
[Symbol.asyncIterator]: ref(1),
171+
[Symbol.hasInstance]: { a: ref('a') },
172+
[Symbol.isConcatSpreadable]: { b: ref(true) },
173+
[Symbol.iterator]: [ref(1)],
174+
[Symbol.match]: new Set<Ref<number>>(),
175+
[Symbol.matchAll]: new Map<number, Ref<string>>(),
176+
[Symbol.replace]: { arr: [ref('a')] },
177+
[Symbol.search]: { set: new Set<Ref<number>>() },
178+
[Symbol.species]: { map: new Map<number, Ref<string>>() },
179+
[Symbol.split]: new WeakSet<Ref<boolean>>(),
180+
[Symbol.toPrimitive]: new WeakMap<Ref<boolean>, string>(),
181+
[Symbol.toStringTag]: { weakSet: new WeakSet<Ref<boolean>>() },
182+
[Symbol.unscopables]: { weakMap: new WeakMap<Ref<boolean>, string>() },
183+
[customSymbol]: { arr: [ref(1)] },
173184
}
174185

175186
const objRef = ref(obj)
176187

177-
expect(objRef.value[Symbol.asyncIterator]).toBe(obj[Symbol.asyncIterator])
178-
expect(objRef.value[Symbol.unscopables]).toBe(obj[Symbol.unscopables])
179-
expect(objRef.value[customSymbol]).toStrictEqual(obj[customSymbol])
188+
const keys: (keyof typeof obj)[] = [
189+
Symbol.asyncIterator,
190+
Symbol.hasInstance,
191+
Symbol.isConcatSpreadable,
192+
Symbol.iterator,
193+
Symbol.match,
194+
Symbol.matchAll,
195+
Symbol.replace,
196+
Symbol.search,
197+
Symbol.species,
198+
Symbol.split,
199+
Symbol.toPrimitive,
200+
Symbol.toStringTag,
201+
Symbol.unscopables,
202+
customSymbol,
203+
]
204+
205+
keys.forEach((key) => {
206+
expect(objRef.value[key]).toStrictEqual(obj[key])
207+
})
180208
})
181209

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

0 commit comments

Comments
 (0)