Skip to content

Commit

Permalink
fix(types): make toRef return correct type(fix vuejs#4732)
Browse files Browse the repository at this point in the history
  • Loading branch information
caozhong1996 committed Oct 4, 2021
1 parent ca17162 commit 3bcfdcd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
10 changes: 10 additions & 0 deletions packages/reactivity/__tests__/ref.spec.ts
Expand Up @@ -268,6 +268,16 @@ describe('reactivity/ref', () => {
// should keep ref
const r = { x: ref(1) }
expect(toRef(r, 'x')).toBe(r.x)

// #4732
const baz = {
foo: {
bar: ref(42)
}
}
const foo = toRef(baz, 'foo')
expect(isRef(foo.value.bar)).toBe(true)
expect(foo.value.bar.value).toBe(42)
})

test('toRefs', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/reactivity/src/ref.ts
Expand Up @@ -65,7 +65,7 @@ export function isRef(r: any): r is Ref {
return Boolean(r && r.__v_isRef === true)
}

export function ref<T extends object>(value: T): ToRef<T>
export function ref<T extends object>(value: T): [T] extends [Ref] ? T : Ref<UnwrapRef<T>>
export function ref<T>(value: T): Ref<UnwrapRef<T>>
export function ref<T = any>(): Ref<T | undefined>
export function ref(value?: unknown) {
Expand Down Expand Up @@ -212,7 +212,7 @@ class ObjectRefImpl<T extends object, K extends keyof T> {
}
}

export type ToRef<T> = [T] extends [Ref] ? T : Ref<UnwrapRef<T>>
export type ToRef<T> = [T] extends [Ref] ? T : Ref<T>
export function toRef<T extends object, K extends keyof T>(
object: T,
key: K
Expand Down

0 comments on commit 3bcfdcd

Please sign in to comment.