Skip to content

Commit

Permalink
fix(types): fix distribution of union types when unwrapping setup bin…
Browse files Browse the repository at this point in the history
…dings (#9909)

close #9903
  • Loading branch information
yangxiuxiu1115 authored Dec 26, 2023
1 parent f96c413 commit 0695c69
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
8 changes: 7 additions & 1 deletion packages/dts-test/ref.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
10 changes: 3 additions & 7 deletions packages/reactivity/src/ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit 0695c69

Please sign in to comment.