Skip to content

Commit 59d53ec

Browse files
committed
fix(types): prevent shallowReactive marker from leaking into value unions
1 parent ea24576 commit 59d53ec

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
reactive,
55
readonly,
66
ref,
7+
shallowReactive,
78
shallowReadonly,
89
} from 'vue'
910
import { describe, expectType } from './utils'
@@ -130,3 +131,12 @@ describe('should not error when assignment', () => {
130131
record2 = arr
131132
expectType<string>(record2[0])
132133
})
134+
135+
describe('shallowReactive marker should not leak into value unions', () => {
136+
const state = shallowReactive({
137+
a: { title: 'A' },
138+
b: { title: 'B' },
139+
})
140+
const value = {} as (typeof state)[keyof typeof state]
141+
expectType<string>(value.title)
142+
})

packages/reactivity/src/reactive.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export function reactive(target: object) {
106106

107107
export declare const ShallowReactiveMarker: unique symbol
108108

109-
export type ShallowReactive<T> = T & { [ShallowReactiveMarker]?: true }
109+
export type ShallowReactive<T> = T & { [ShallowReactiveMarker]: never }
110110

111111
/**
112112
* Shallow version of {@link reactive}.

packages/reactivity/src/ref.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -548,8 +548,10 @@ export type UnwrapRefSimple<T> = T extends
548548
? WeakSet<UnwrapRefSimple<V>> & UnwrapRef<Omit<T, keyof WeakSet<any>>>
549549
: T extends ReadonlyArray<any>
550550
? { [K in keyof T]: UnwrapRefSimple<T[K]> }
551-
: T extends object & { [ShallowReactiveMarker]?: never }
552-
? {
553-
[P in keyof T]: P extends symbol ? T[P] : UnwrapRef<T[P]>
554-
}
555-
: T
551+
: T extends object & { [ShallowReactiveMarker]: never }
552+
? T
553+
: T extends object
554+
? {
555+
[P in keyof T]: P extends symbol ? T[P] : UnwrapRef<T[P]>
556+
}
557+
: T

0 commit comments

Comments
 (0)