From: Evan You Date: Fri, 16 Aug 2019 13:54:57 +0000 (-0400) Subject: wip: improve ref typing X-Git-Tag: v3.0.0-alpha.0~925 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=96d65e1ab58ee2e1e820e41fbc2a7c753555cdc2;p=thirdparty%2Fvuejs%2Fcore.git wip: improve ref typing --- diff --git a/packages/reactivity/__tests__/ref.spec.ts b/packages/reactivity/__tests__/ref.spec.ts index 94600eb1c4..e5cff502f9 100644 --- a/packages/reactivity/__tests__/ref.spec.ts +++ b/packages/reactivity/__tests__/ref.spec.ts @@ -61,4 +61,14 @@ describe('observer/value', () => { expect(dummy2).toBe(3) expect(dummy3).toBe(3) }) + + it('should unwrap nested values in types', () => { + const a = { + b: ref(0) + } + + const c = ref(a) + + expect(typeof (c.value.b + 1)).toBe('number') + }) }) diff --git a/packages/reactivity/src/ref.ts b/packages/reactivity/src/ref.ts index a3a67b8682..d0dc26fed9 100644 --- a/packages/reactivity/src/ref.ts +++ b/packages/reactivity/src/ref.ts @@ -6,7 +6,7 @@ import { reactive } from './reactive' export const knownValues = new WeakSet() export interface Ref { - value: T + value: T extends Ref ? Ref : UnwrapRef } const convert = (val: any): any => (isObject(val) ? reactive(val) : val) @@ -24,7 +24,7 @@ export function ref(raw: T): Ref { } } knownValues.add(v) - return v + return v as any } export function isRef(v: any): v is Ref {