From 0695c69e0dfaf99882a623fe75b433c9618ea648 Mon Sep 17 00:00:00 2001 From: yangxiuxiu <79584569+RicardoErii@users.noreply.github.com> Date: Tue, 26 Dec 2023 11:57:50 +0800 Subject: [PATCH] fix(types): fix distribution of union types when unwrapping setup bindings (#9909) close #9903 --- packages/dts-test/ref.test-d.ts | 8 +++++++- packages/reactivity/src/ref.ts | 10 +++------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/packages/dts-test/ref.test-d.ts b/packages/dts-test/ref.test-d.ts index 62bad77c2ee..fc3c9e65e4c 100644 --- a/packages/dts-test/ref.test-d.ts +++ b/packages/dts-test/ref.test-d.ts @@ -244,13 +244,19 @@ expectType(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(p2.a) +expectType(p2.c) +expectType(p2.u) expectType>(p2.obj.k) +expectType<{ name: string } | null>(p2.union) // toRef and toRefs { diff --git a/packages/reactivity/src/ref.ts b/packages/reactivity/src/ref.ts index e156a2e1134..6a584090efc 100644 --- a/packages/reactivity/src/ref.ts +++ b/packages/reactivity/src/ref.ts @@ -477,15 +477,11 @@ type BaseTypes = string | number | boolean export interface RefUnwrapBailTypes {} export type ShallowUnwrapRef = { - [K in keyof T]: T[K] extends Ref - ? V // if `V` is `unknown` that means it does not extend `Ref` and is undefined - : T[K] extends Ref | undefined - ? unknown extends V - ? undefined - : V | undefined - : T[K] + [K in keyof T]: DistrubuteRef } +type DistrubuteRef = T extends Ref ? V : T + export type UnwrapRef = T extends ShallowRef ? V : T extends Ref