소스 검색

fix(types): prevent shallowReactive marker from leaking into value unions (#14493)

close #14490
edison 4 주 전
부모
커밋
3b561db4ab
3개의 변경된 파일18개의 추가작업 그리고 6개의 파일을 삭제
  1. 10 0
      packages-private/dts-test/reactivity.test-d.ts
  2. 1 1
      packages/reactivity/src/reactive.ts
  3. 7 5
      packages/reactivity/src/ref.ts

+ 10 - 0
packages-private/dts-test/reactivity.test-d.ts

@@ -4,6 +4,7 @@ import {
   reactive,
   readonly,
   ref,
+  shallowReactive,
   shallowReadonly,
 } from 'vue'
 import { describe, expectType } from './utils'
@@ -130,3 +131,12 @@ describe('should not error when assignment', () => {
   record2 = arr
   expectType<string>(record2[0])
 })
+
+describe('shallowReactive marker should not leak into value unions', () => {
+  const state = shallowReactive({
+    a: { title: 'A' },
+    b: { title: 'B' },
+  })
+  const value = {} as (typeof state)[keyof typeof state]
+  expectType<string>(value.title)
+})

+ 1 - 1
packages/reactivity/src/reactive.ts

@@ -106,7 +106,7 @@ export function reactive(target: object) {
 
 export declare const ShallowReactiveMarker: unique symbol
 
-export type ShallowReactive<T> = T & { [ShallowReactiveMarker]?: true }
+export type ShallowReactive<T> = T & { [ShallowReactiveMarker]: never }
 
 /**
  * Shallow version of {@link reactive}.

+ 7 - 5
packages/reactivity/src/ref.ts

@@ -567,8 +567,10 @@ export type UnwrapRefSimple<T> = T extends
           ? WeakSet<UnwrapRefSimple<V>> & UnwrapRef<Omit<T, keyof WeakSet<any>>>
           : T extends ReadonlyArray<any>
             ? { [K in keyof T]: UnwrapRefSimple<T[K]> }
-            : T extends object & { [ShallowReactiveMarker]?: never }
-              ? {
-                  [P in keyof T]: P extends symbol ? T[P] : UnwrapRef<T[P]>
-                }
-              : T
+            : T extends object & { [ShallowReactiveMarker]: never }
+              ? T
+              : T extends object
+                ? {
+                    [P in keyof T]: P extends symbol ? T[P] : UnwrapRef<T[P]>
+                  }
+                : T