From 350ceb365fc1bdcfc8934e586ff35524b2e16e31 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Sat, 16 Dec 2023 23:02:38 +0100 Subject: [PATCH] fix(types): extract properties from extended collections Fix #9852 --- packages/dts-test/reactivity.test-d.ts | 23 +++++++++++++++++++++++ packages/reactivity/src/ref.ts | 9 +++++---- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/packages/dts-test/reactivity.test-d.ts b/packages/dts-test/reactivity.test-d.ts index 431bbafbdf4..e0d8e207f0d 100644 --- a/packages/dts-test/reactivity.test-d.ts +++ b/packages/dts-test/reactivity.test-d.ts @@ -77,6 +77,18 @@ describe('should unwrap Map correctly', () => { expectType(wm2.get({})!.wrap) }) +describe('should unwrap extended Map correctly', () => { + class ExtendendMap1 extends Map }> { + foo = ref('foo') + bar = 1 + } + + const emap1 = reactive(new ExtendendMap1()) + expectType(emap1.foo) + expectType(emap1.bar) + expectType(emap1.get('a')!.wrap) +}) + describe('should unwrap Set correctly', () => { const set = reactive(new Set>()) expectType>>(set) @@ -90,3 +102,14 @@ describe('should unwrap Set correctly', () => { const ws2 = reactive(new WeakSet<{ wrap: Ref }>()) expectType>(ws2) }) + +describe('should unwrap extended Set correctly', () => { + class ExtendendSet1 extends Set<{ wrap: Ref }> { + foo = ref('foo') + bar = 1 + } + + const eset1 = reactive(new ExtendendSet1()) + expectType(eset1.foo) + expectType(eset1.bar) +}) diff --git a/packages/reactivity/src/ref.ts b/packages/reactivity/src/ref.ts index a85c79c0bc6..e156a2e1134 100644 --- a/packages/reactivity/src/ref.ts +++ b/packages/reactivity/src/ref.ts @@ -500,13 +500,14 @@ export type UnwrapRefSimple = T extends | { [RawSymbol]?: true } ? T : T extends Map - ? Map> + ? Map> & UnwrapRef>> : T extends WeakMap - ? WeakMap> + ? WeakMap> & + UnwrapRef>> : T extends Set - ? Set> + ? Set> & UnwrapRef>> : T extends WeakSet - ? WeakSet> + ? WeakSet> & UnwrapRef>> : T extends ReadonlyArray ? { [K in keyof T]: UnwrapRefSimple } : T extends object & { [ShallowReactiveMarker]?: never }