Skip to content

Commit 0695c69

Browse files
fix(types): fix distribution of union types when unwrapping setup bindings (#9909)
close #9903
1 parent f96c413 commit 0695c69

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -244,13 +244,19 @@ expectType<typeof r1>(p1)
244244
// proxyRefs: `ShallowUnwrapRef`
245245
const r2 = {
246246
a: ref(1),
247+
c: computed(() => 1),
248+
u: undefined,
247249
obj: {
248250
k: ref('foo')
249-
}
251+
},
252+
union: Math.random() > 0 - 5 ? ref({ name: 'yo' }) : null
250253
}
251254
const p2 = proxyRefs(r2)
252255
expectType<number>(p2.a)
256+
expectType<number>(p2.c)
257+
expectType<undefined>(p2.u)
253258
expectType<Ref<string>>(p2.obj.k)
259+
expectType<{ name: string } | null>(p2.union)
254260

255261
// toRef and toRefs
256262
{

packages/reactivity/src/ref.ts

+3-7
Original file line numberDiff line numberDiff line change
@@ -477,15 +477,11 @@ type BaseTypes = string | number | boolean
477477
export interface RefUnwrapBailTypes {}
478478

479479
export type ShallowUnwrapRef<T> = {
480-
[K in keyof T]: T[K] extends Ref<infer V>
481-
? V // if `V` is `unknown` that means it does not extend `Ref` and is undefined
482-
: T[K] extends Ref<infer V> | undefined
483-
? unknown extends V
484-
? undefined
485-
: V | undefined
486-
: T[K]
480+
[K in keyof T]: DistrubuteRef<T[K]>
487481
}
488482

483+
type DistrubuteRef<T> = T extends Ref<infer V> ? V : T
484+
489485
export type UnwrapRef<T> = T extends ShallowRef<infer V>
490486
? V
491487
: T extends Ref<infer V>

0 commit comments

Comments
 (0)