]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(types): fix distribution of union types when unwrapping setup bindings (#9909)
authoryangxiuxiu <79584569+RicardoErii@users.noreply.github.com>
Tue, 26 Dec 2023 03:57:50 +0000 (11:57 +0800)
committerGitHub <noreply@github.com>
Tue, 26 Dec 2023 03:57:50 +0000 (11:57 +0800)
close #9903

packages/dts-test/ref.test-d.ts
packages/reactivity/src/ref.ts

index 62bad77c2ee80c2f19562a049491c7d606e312f2..fc3c9e65e4c8b3fbbc1630383e9bfe5318097c0d 100644 (file)
@@ -244,13 +244,19 @@ expectType<typeof r1>(p1)
 // proxyRefs: `ShallowUnwrapRef`
 const r2 = {
   a: ref(1),
+  c: computed(() => 1),
+  u: undefined,
   obj: {
     k: ref('foo')
-  }
+  },
+  union: Math.random() > 0 - 5 ? ref({ name: 'yo' }) : null
 }
 const p2 = proxyRefs(r2)
 expectType<number>(p2.a)
+expectType<number>(p2.c)
+expectType<undefined>(p2.u)
 expectType<Ref<string>>(p2.obj.k)
+expectType<{ name: string } | null>(p2.union)
 
 // toRef and toRefs
 {
index e156a2e1134a42bb4985a236256ec696a0ea7e05..6a584090efc975b7d2fc14509c9427a1cb5bca5c 100644 (file)
@@ -477,15 +477,11 @@ type BaseTypes = string | number | boolean
 export interface RefUnwrapBailTypes {}
 
 export type ShallowUnwrapRef<T> = {
-  [K in keyof T]: T[K] extends Ref<infer V>
-    ? V // if `V` is `unknown` that means it does not extend `Ref` and is undefined
-    : T[K] extends Ref<infer V> | undefined
-      ? unknown extends V
-        ? undefined
-        : V | undefined
-      : T[K]
+  [K in keyof T]: DistrubuteRef<T[K]>
 }
 
+type DistrubuteRef<T> = T extends Ref<infer V> ? V : T
+
 export type UnwrapRef<T> = T extends ShallowRef<infer V>
   ? V
   : T extends Ref<infer V>