Skip to content

fix(types): unwrap custom symbols in Reactive #13740

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages-private/dts-test/reactivity.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,16 @@ describe('should not error when assignment', () => {
record2 = arr
expectType<string>(record2[0])
})

describe('unwraps custom symbols', () => {
const customSymbol = Symbol('custom')
const obj = reactive({
[customSymbol]: ref(1),
foo: ref(2),
[Symbol.toStringTag]: ref(3),
})

expectType<number>(obj[customSymbol])
expectType<number>(obj.foo)
expectType<Ref<number, number>>(obj[Symbol.toStringTag])
})
4 changes: 4 additions & 0 deletions packages/reactivity/src/baseHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion packages/reactivity/src/ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -517,6 +518,8 @@ export type UnwrapRefSimple<T> = T extends
? { [K in keyof T]: UnwrapRefSimple<T[K]> }
: T extends object & { [ShallowReactiveMarker]?: never }
? {
[P in keyof T]: P extends symbol ? T[P] : UnwrapRef<T[P]>
[P in keyof T]: P extends BuiltinSymbols
? T[P]
: UnwrapRef<T[P]>
}
: T
Loading