diff --git a/packages-private/dts-test/reactivity.test-d.ts b/packages-private/dts-test/reactivity.test-d.ts index f5b9176729c..93b07e5e8f8 100644 --- a/packages-private/dts-test/reactivity.test-d.ts +++ b/packages-private/dts-test/reactivity.test-d.ts @@ -130,3 +130,16 @@ describe('should not error when assignment', () => { record2 = arr expectType(record2[0]) }) + +describe('unwraps custom symbols', () => { + const customSymbol = Symbol('custom') + const obj = reactive({ + [customSymbol]: ref(1), + foo: ref(2), + [Symbol.toStringTag]: ref(3), + }) + + expectType(obj[customSymbol]) + expectType(obj.foo) + expectType>(obj[Symbol.toStringTag]) +}) diff --git a/packages/reactivity/src/baseHandlers.ts b/packages/reactivity/src/baseHandlers.ts index faec3012f40..853d9a209f6 100644 --- a/packages/reactivity/src/baseHandlers.ts +++ b/packages/reactivity/src/baseHandlers.ts @@ -27,6 +27,10 @@ import { warn } from './warning' const isNonTrackableKeys = /*@__PURE__*/ makeMap(`__proto__,__v_isRef,__isVue`) +export type BuiltinSymbols = Extract< + (typeof Symbol)[keyof typeof Symbol], + symbol +> const builtInSymbols = new Set( /*@__PURE__*/ Object.getOwnPropertyNames(Symbol) diff --git a/packages/reactivity/src/ref.ts b/packages/reactivity/src/ref.ts index 59b713dd862..dca613272e0 100644 --- a/packages/reactivity/src/ref.ts +++ b/packages/reactivity/src/ref.ts @@ -19,6 +19,7 @@ import { import type { ComputedRef, WritableComputedRef } from './computed' import { ReactiveFlags, TrackOpTypes, TriggerOpTypes } from './constants' import { warn } from './warning' +import type { BuiltinSymbols } from './baseHandlers' declare const RefSymbol: unique symbol export declare const RawSymbol: unique symbol @@ -517,6 +518,8 @@ export type UnwrapRefSimple = T extends ? { [K in keyof T]: UnwrapRefSimple } : T extends object & { [ShallowReactiveMarker]?: never } ? { - [P in keyof T]: P extends symbol ? T[P] : UnwrapRef + [P in keyof T]: P extends BuiltinSymbols + ? T[P] + : UnwrapRef } : T