From a85f3cd2106d7348d25e23d005d168d01afda6a2 Mon Sep 17 00:00:00 2001 From: webfansplz <> Date: Thu, 29 Jul 2021 09:51:02 +0800 Subject: [PATCH] refactor(types): Simplify the SymbolExtract --- src/reactivity/ref.ts | 34 +++-------------------------- test-dts/ref.test-d.ts | 38 +++++++++++++++++++++++++++----- test/v3/reactivity/ref.spec.ts | 40 +++++++++++++++++++++++++++++----- 3 files changed, 69 insertions(+), 43 deletions(-) diff --git a/src/reactivity/ref.ts b/src/reactivity/ref.ts index 14c7b652..455cf3b6 100644 --- a/src/reactivity/ref.ts +++ b/src/reactivity/ref.ts @@ -34,39 +34,11 @@ type UnwrapRefSimple = T extends Function | CollectionTypes | BaseTypes | Ref : T extends Array ? { [K in keyof T]: UnwrapRefSimple } : T extends object - ? UnwrappedObject + ? { + [P in keyof T]: P extends symbol ? T[P] : UnwrapRef + } : T -// Extract all known symbols from an object -// when unwrapping Object the symbols are not `in keyof`, this should cover all the -// known symbols -type SymbolExtract = (T extends { [Symbol.asyncIterator]: infer V } - ? { [Symbol.asyncIterator]: V } - : {}) & - (T extends { [Symbol.hasInstance]: infer V } - ? { [Symbol.hasInstance]: V } - : {}) & - (T extends { [Symbol.isConcatSpreadable]: infer V } - ? { [Symbol.isConcatSpreadable]: V } - : {}) & - (T extends { [Symbol.iterator]: infer V } ? { [Symbol.iterator]: V } : {}) & - (T extends { [Symbol.match]: infer V } ? { [Symbol.match]: V } : {}) & - (T extends { [Symbol.replace]: infer V } ? { [Symbol.replace]: V } : {}) & - (T extends { [Symbol.search]: infer V } ? { [Symbol.search]: V } : {}) & - (T extends { [Symbol.species]: infer V } ? { [Symbol.species]: V } : {}) & - (T extends { [Symbol.split]: infer V } ? { [Symbol.split]: V } : {}) & - (T extends { [Symbol.toPrimitive]: infer V } - ? { [Symbol.toPrimitive]: V } - : {}) & - (T extends { [Symbol.toStringTag]: infer V } - ? { [Symbol.toStringTag]: V } - : {}) & - (T extends { [Symbol.unscopables]: infer V } - ? { [Symbol.unscopables]: V } - : {}) - -type UnwrappedObject = { [P in keyof T]: UnwrapRef } & SymbolExtract - interface RefOption { get(): T set?(x: T): void diff --git a/test-dts/ref.test-d.ts b/test-dts/ref.test-d.ts index 79d5f434..374b0398 100644 --- a/test-dts/ref.test-d.ts +++ b/test-dts/ref.test-d.ts @@ -78,16 +78,42 @@ bailType(el) function withSymbol() { const customSymbol = Symbol() const obj = { - [Symbol.asyncIterator]: { a: 1 }, - [Symbol.unscopables]: { b: '1' }, - [customSymbol]: { c: [1, 2, 3] }, + [Symbol.asyncIterator]: ref(1), + [Symbol.hasInstance]: { a: ref('a') }, + [Symbol.isConcatSpreadable]: { b: ref(true) }, + [Symbol.iterator]: [ref(1)], + [Symbol.match]: new Set>(), + [Symbol.matchAll]: new Map>(), + [Symbol.replace]: { arr: [ref('a')] }, + [Symbol.search]: { set: new Set>() }, + [Symbol.species]: { map: new Map>() }, + [Symbol.split]: new WeakSet>(), + [Symbol.toPrimitive]: new WeakMap, string>(), + [Symbol.toStringTag]: { weakSet: new WeakSet>() }, + [Symbol.unscopables]: { weakMap: new WeakMap, string>() }, + [customSymbol]: { arr: [ref(1)] }, } const objRef = ref(obj) - expectType<{ a: number }>(objRef.value[Symbol.asyncIterator]) - expectType<{ b: string }>(objRef.value[Symbol.unscopables]) - expectType<{ c: Array }>(objRef.value[customSymbol]) + expectType>(objRef.value[Symbol.asyncIterator]) + expectType<{ a: Ref }>(objRef.value[Symbol.hasInstance]) + expectType<{ b: Ref }>(objRef.value[Symbol.isConcatSpreadable]) + expectType[]>(objRef.value[Symbol.iterator]) + expectType>>(objRef.value[Symbol.match]) + expectType>>(objRef.value[Symbol.matchAll]) + expectType<{ arr: Ref[] }>(objRef.value[Symbol.replace]) + expectType<{ set: Set> }>(objRef.value[Symbol.search]) + expectType<{ map: Map> }>(objRef.value[Symbol.species]) + expectType>>(objRef.value[Symbol.split]) + expectType, string>>(objRef.value[Symbol.toPrimitive]) + expectType<{ weakSet: WeakSet> }>( + objRef.value[Symbol.toStringTag] + ) + expectType<{ weakMap: WeakMap, string> }>( + objRef.value[Symbol.unscopables] + ) + expectType<{ arr: Ref[] }>(objRef.value[customSymbol]) } withSymbol() diff --git a/test/v3/reactivity/ref.spec.ts b/test/v3/reactivity/ref.spec.ts index 26f80780..5f5a3371 100644 --- a/test/v3/reactivity/ref.spec.ts +++ b/test/v3/reactivity/ref.spec.ts @@ -167,16 +167,44 @@ describe('reactivity/ref', () => { it('should keep symbols', () => { const customSymbol = Symbol() const obj = { - [Symbol.asyncIterator]: { a: 1 }, - [Symbol.unscopables]: { b: '1' }, - [customSymbol]: { c: [1, 2, 3] }, + [Symbol.asyncIterator]: ref(1), + [Symbol.hasInstance]: { a: ref('a') }, + [Symbol.isConcatSpreadable]: { b: ref(true) }, + [Symbol.iterator]: [ref(1)], + [Symbol.match]: new Set>(), + [Symbol.matchAll]: new Map>(), + [Symbol.replace]: { arr: [ref('a')] }, + [Symbol.search]: { set: new Set>() }, + [Symbol.species]: { map: new Map>() }, + [Symbol.split]: new WeakSet>(), + [Symbol.toPrimitive]: new WeakMap, string>(), + [Symbol.toStringTag]: { weakSet: new WeakSet>() }, + [Symbol.unscopables]: { weakMap: new WeakMap, string>() }, + [customSymbol]: { arr: [ref(1)] }, } const objRef = ref(obj) - expect(objRef.value[Symbol.asyncIterator]).toBe(obj[Symbol.asyncIterator]) - expect(objRef.value[Symbol.unscopables]).toBe(obj[Symbol.unscopables]) - expect(objRef.value[customSymbol]).toStrictEqual(obj[customSymbol]) + const keys: (keyof typeof obj)[] = [ + Symbol.asyncIterator, + Symbol.hasInstance, + Symbol.isConcatSpreadable, + Symbol.iterator, + Symbol.match, + Symbol.matchAll, + Symbol.replace, + Symbol.search, + Symbol.species, + Symbol.split, + Symbol.toPrimitive, + Symbol.toStringTag, + Symbol.unscopables, + customSymbol, + ] + + keys.forEach((key) => { + expect(objRef.value[key]).toStrictEqual(obj[key]) + }) }) test('unref', () => {