Skip to content

Commit 5ca3dba

Browse files
committed
fix(reactivity): reactive collection types
closes #8904
1 parent fc772db commit 5ca3dba

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

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

+28
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,31 @@ describe('should unwrap tuple correctly', () => {
6262
const reactiveTuple = reactive(tuple)
6363
expectType<Ref<number>>(reactiveTuple[0])
6464
})
65+
66+
describe('should unwrap Map correctly', () => {
67+
const map = reactive(new Map<string, Ref<number>>())
68+
expectType<Ref<number>>(map.get('a')!)
69+
70+
const map2 = reactive(new Map<string, { wrap: Ref<number> }>())
71+
expectType<number>(map2.get('a')!.wrap)
72+
73+
const wm = reactive(new WeakMap<object, Ref<number>>())
74+
expectType<Ref<number>>(wm.get({})!)
75+
76+
const wm2 = reactive(new WeakMap<object, { wrap: Ref<number> }>())
77+
expectType<number>(wm2.get({})!.wrap)
78+
})
79+
80+
describe('should unwrap Set correctly', () => {
81+
const set = reactive(new Set<Ref<number>>())
82+
expectType<Set<Ref<number>>>(set)
83+
84+
const set2 = reactive(new Set<{ wrap: Ref<number> }>())
85+
expectType<Set<{ wrap: number }>>(set2)
86+
87+
const ws = reactive(new WeakSet<Ref<number>>())
88+
expectType<WeakSet<Ref<number>>>(ws)
89+
90+
const ws2 = reactive(new WeakSet<{ wrap: Ref<number> }>())
91+
expectType<WeakSet<{ wrap: number }>>(ws2)
92+
})

packages/reactivity/src/collectionHandlers.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { track, trigger, ITERATE_KEY, MAP_KEY_ITERATE_KEY } from './effect'
33
import { TrackOpTypes, TriggerOpTypes } from './operations'
44
import { capitalize, hasOwn, hasChanged, toRawType, isMap } from '@vue/shared'
55

6-
export type CollectionTypes = IterableCollections | WeakCollections
6+
type CollectionTypes = IterableCollections | WeakCollections
77

88
type IterableCollections = Map<any, any> | Set<any>
99
type WeakCollections = WeakMap<any, any> | WeakSet<any>

packages/reactivity/src/ref.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import {
1717
isShallow
1818
} from './reactive'
1919
import type { ShallowReactiveMarker } from './reactive'
20-
import { CollectionTypes } from './collectionHandlers'
2120
import { createDep, Dep } from './dep'
2221

2322
declare const RefSymbol: unique symbol
@@ -492,12 +491,19 @@ export type UnwrapRef<T> = T extends ShallowRef<infer V>
492491

493492
export type UnwrapRefSimple<T> = T extends
494493
| Function
495-
| CollectionTypes
496494
| BaseTypes
497495
| Ref
498496
| RefUnwrapBailTypes[keyof RefUnwrapBailTypes]
499497
| { [RawSymbol]?: true }
500498
? T
499+
: T extends Map<infer K, infer V>
500+
? Map<K, UnwrapRefSimple<V>>
501+
: T extends WeakMap<infer K, infer V>
502+
? WeakMap<K, UnwrapRefSimple<V>>
503+
: T extends Set<infer V>
504+
? Set<UnwrapRefSimple<V>>
505+
: T extends WeakSet<infer V>
506+
? WeakSet<UnwrapRefSimple<V>>
501507
: T extends ReadonlyArray<any>
502508
? { [K in keyof T]: UnwrapRefSimple<T[K]> }
503509
: T extends object & { [ShallowReactiveMarker]?: never }

0 commit comments

Comments
 (0)