Skip to content

Commit

Permalink
chore: test + simplify fix
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Dec 26, 2023
1 parent 18b3c56 commit fa5bf44
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 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
11 changes: 3 additions & 8 deletions packages/reactivity/src/ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -476,17 +476,12 @@ type BaseTypes = string | number | boolean
*/
export interface RefUnwrapBailTypes {}

type DistrubuteRef<T> = T extends Ref<infer V>
? unknown extends V
? never
: V
: T
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
: DistrubuteRef<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 fa5bf44

Please sign in to comment.