Skip to content

Commit 24b1c1d

Browse files
authored
fix(types): extract properties from extended collections (#9854)
close #9852
1 parent 04d2c05 commit 24b1c1d

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

packages/dts-test/reactivity.test-d.ts

+23
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,18 @@ describe('should unwrap Map correctly', () => {
7777
expectType<number>(wm2.get({})!.wrap)
7878
})
7979

80+
describe('should unwrap extended Map correctly', () => {
81+
class ExtendendMap1 extends Map<string, { wrap: Ref<number> }> {
82+
foo = ref('foo')
83+
bar = 1
84+
}
85+
86+
const emap1 = reactive(new ExtendendMap1())
87+
expectType<string>(emap1.foo)
88+
expectType<number>(emap1.bar)
89+
expectType<number>(emap1.get('a')!.wrap)
90+
})
91+
8092
describe('should unwrap Set correctly', () => {
8193
const set = reactive(new Set<Ref<number>>())
8294
expectType<Set<Ref<number>>>(set)
@@ -90,3 +102,14 @@ describe('should unwrap Set correctly', () => {
90102
const ws2 = reactive(new WeakSet<{ wrap: Ref<number> }>())
91103
expectType<WeakSet<{ wrap: number }>>(ws2)
92104
})
105+
106+
describe('should unwrap extended Set correctly', () => {
107+
class ExtendendSet1 extends Set<{ wrap: Ref<number> }> {
108+
foo = ref('foo')
109+
bar = 1
110+
}
111+
112+
const eset1 = reactive(new ExtendendSet1())
113+
expectType<string>(eset1.foo)
114+
expectType<number>(eset1.bar)
115+
})

packages/reactivity/src/ref.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -500,13 +500,14 @@ export type UnwrapRefSimple<T> = T extends
500500
| { [RawSymbol]?: true }
501501
? T
502502
: T extends Map<infer K, infer V>
503-
? Map<K, UnwrapRefSimple<V>>
503+
? Map<K, UnwrapRefSimple<V>> & UnwrapRef<Omit<T, keyof Map<any, any>>>
504504
: T extends WeakMap<infer K, infer V>
505-
? WeakMap<K, UnwrapRefSimple<V>>
505+
? WeakMap<K, UnwrapRefSimple<V>> &
506+
UnwrapRef<Omit<T, keyof WeakMap<any, any>>>
506507
: T extends Set<infer V>
507-
? Set<UnwrapRefSimple<V>>
508+
? Set<UnwrapRefSimple<V>> & UnwrapRef<Omit<T, keyof Set<any>>>
508509
: T extends WeakSet<infer V>
509-
? WeakSet<UnwrapRefSimple<V>>
510+
? WeakSet<UnwrapRefSimple<V>> & UnwrapRef<Omit<T, keyof WeakSet<any>>>
510511
: T extends ReadonlyArray<any>
511512
? { [K in keyof T]: UnwrapRefSimple<T[K]> }
512513
: T extends object & { [ShallowReactiveMarker]?: never }

0 commit comments

Comments
 (0)