fix(types): fix distribution of union types when unwrapping setup bindings (#9909)

close #9903
This commit is contained in:
yangxiuxiu 2023-12-26 11:57:50 +08:00 committed by GitHub
parent f96c413e8e
commit 0695c69e0d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 8 deletions

View File

@ -244,13 +244,19 @@ expectType<typeof r1>(p1)
// proxyRefs: `ShallowUnwrapRef` // proxyRefs: `ShallowUnwrapRef`
const r2 = { const r2 = {
a: ref(1), a: ref(1),
c: computed(() => 1),
u: undefined,
obj: { obj: {
k: ref('foo') k: ref('foo')
} },
union: Math.random() > 0 - 5 ? ref({ name: 'yo' }) : null
} }
const p2 = proxyRefs(r2) const p2 = proxyRefs(r2)
expectType<number>(p2.a) expectType<number>(p2.a)
expectType<number>(p2.c)
expectType<undefined>(p2.u)
expectType<Ref<string>>(p2.obj.k) expectType<Ref<string>>(p2.obj.k)
expectType<{ name: string } | null>(p2.union)
// toRef and toRefs // toRef and toRefs
{ {

View File

@ -477,15 +477,11 @@ type BaseTypes = string | number | boolean
export interface RefUnwrapBailTypes {} export interface RefUnwrapBailTypes {}
export type ShallowUnwrapRef<T> = { export type ShallowUnwrapRef<T> = {
[K in keyof T]: T[K] extends Ref<infer V> [K in keyof T]: DistrubuteRef<T[K]>
? 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]
} }
type DistrubuteRef<T> = T extends Ref<infer V> ? V : T
export type UnwrapRef<T> = T extends ShallowRef<infer V> export type UnwrapRef<T> = T extends ShallowRef<infer V>
? V ? V
: T extends Ref<infer V> : T extends Ref<infer V>