From 24b1c1dd57fd55d998aa231a147500e010b10219 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Tue, 19 Dec 2023 10:05:29 +0100 Subject: [PATCH] fix(types): extract properties from extended collections (#9854) close #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 }