]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
wip: improve ref typing
authorEvan You <yyx990803@gmail.com>
Fri, 16 Aug 2019 13:54:57 +0000 (09:54 -0400)
committerEvan You <yyx990803@gmail.com>
Fri, 16 Aug 2019 13:54:57 +0000 (09:54 -0400)
packages/reactivity/__tests__/ref.spec.ts
packages/reactivity/src/ref.ts

index 94600eb1c403b4bf879484fb33a2940c12ff6a91..e5cff502f958d4959c9d35b05bfaf49ebef6bc9f 100644 (file)
@@ -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')
+  })
 })
index a3a67b86821c0967c88ae72fa8f277355ad64763..d0dc26fed971a0e248c2a94d810fa481b4fa26fc 100644 (file)
@@ -6,7 +6,7 @@ import { reactive } from './reactive'
 export const knownValues = new WeakSet()
 
 export interface Ref<T> {
-  value: T
+  value: T extends Ref<infer V> ? Ref<V> : UnwrapRef<T>
 }
 
 const convert = (val: any): any => (isObject(val) ? reactive(val) : val)
@@ -24,7 +24,7 @@ export function ref<T>(raw: T): Ref<T> {
     }
   }
   knownValues.add(v)
-  return v
+  return v as any
 }
 
 export function isRef(v: any): v is Ref<any> {