Skip to content

Commit 1d247f4

Browse files
committed
fix(types): unwrap custom symbols in Reactive
1 parent c875019 commit 1d247f4

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,14 @@ describe('should not error when assignment', () => {
130130
record2 = arr
131131
expectType<string>(record2[0])
132132
})
133+
134+
describe('unwraps custom symbols', () => {
135+
const customSymbol = Symbol('custom')
136+
const obj = reactive({
137+
[customSymbol]: ref(1),
138+
foo: ref(2),
139+
})
140+
141+
expectType<number>(obj[customSymbol])
142+
expectType<number>(obj.foo)
143+
})

packages/reactivity/src/baseHandlers.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ import { warn } from './warning'
2727

2828
const isNonTrackableKeys = /*@__PURE__*/ makeMap(`__proto__,__v_isRef,__isVue`)
2929

30+
export type BuiltinSymbols = Extract<
31+
(typeof Symbol)[keyof typeof Symbol],
32+
symbol
33+
>
3034
const builtInSymbols = new Set(
3135
/*@__PURE__*/
3236
Object.getOwnPropertyNames(Symbol)

packages/reactivity/src/ref.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
import type { ComputedRef, WritableComputedRef } from './computed'
2020
import { ReactiveFlags, TrackOpTypes, TriggerOpTypes } from './constants'
2121
import { warn } from './warning'
22+
import type { BuiltinSymbols } from './baseHandlers'
2223

2324
declare const RefSymbol: unique symbol
2425
export declare const RawSymbol: unique symbol
@@ -517,6 +518,8 @@ export type UnwrapRefSimple<T> = T extends
517518
? { [K in keyof T]: UnwrapRefSimple<T[K]> }
518519
: T extends object & { [ShallowReactiveMarker]?: never }
519520
? {
520-
[P in keyof T]: P extends symbol ? T[P] : UnwrapRef<T[P]>
521+
[P in keyof T]: P extends BuiltinSymbols
522+
? T[P]
523+
: UnwrapRef<T[P]>
521524
}
522525
: T

0 commit comments

Comments
 (0)