]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(types): ref value type unwrapping should happen at creation time
authorEvan You <yyx990803@gmail.com>
Wed, 26 Feb 2020 00:44:06 +0000 (19:44 -0500)
committerEvan You <yyx990803@gmail.com>
Wed, 26 Feb 2020 00:44:06 +0000 (19:44 -0500)
packages/reactivity/src/computed.ts
packages/reactivity/src/ref.ts
test-dts/ref.test-d.ts
test-dts/watch.test-d.ts

index 1a64bdf2a630df35eae43b71e231fd0b2a9c8cd1..154247a672c95186121b7707c46dcfdd38070af7 100644 (file)
@@ -1,10 +1,10 @@
 import { effect, ReactiveEffect, trigger, track } from './effect'
 import { TriggerOpTypes, TrackOpTypes } from './operations'
-import { Ref, UnwrapRef } from './ref'
+import { Ref } from './ref'
 import { isFunction, NOOP } from '@vue/shared'
 
 export interface ComputedRef<T = any> extends WritableComputedRef<T> {
-  readonly value: UnwrapRef<T>
+  readonly value: T
 }
 
 export interface WritableComputedRef<T> extends Ref<T> {
index f88631130e755572317004fb806b178b46accbc5..dde3b82b7d630fd9ddda8beeb18042ef3064c607 100644 (file)
@@ -17,7 +17,7 @@ export interface Ref<T = any> {
   // don't want this internal field to leak into userland autocompletion -
   // a private symbol, on the other hand, achieves just that.
   [isRefSymbol]: true
-  value: UnwrapRef<T>
+  value: T
 }
 
 const convert = <T extends unknown>(val: T): T =>
@@ -28,13 +28,13 @@ export function isRef(r: any): r is Ref {
   return r ? r._isRef === true : false
 }
 
-export function ref<T>(value: T): T extends Ref ? T : Ref<T>
+export function ref<T>(value: T): T extends Ref ? T : Ref<UnwrapRef<T>>
 export function ref<T = any>(): Ref<T>
 export function ref(value?: unknown) {
   return createRef(value)
 }
 
-export function shallowRef<T>(value: T): T extends Ref ? T : Ref<T>
+export function shallowRef<T>(value: T): T extends Ref ? T : Ref<UnwrapRef<T>>
 export function shallowRef<T = any>(): Ref<T>
 export function shallowRef(value?: unknown) {
   return createRef(value, true)
index e1323fef1fb44312bb9dc5f0a8ae3a1144ae4766..1ae7c355a45b0d9e7d51107ec764053e9dea524e 100644 (file)
@@ -13,6 +13,12 @@ function foo(arg: number | Ref<number>) {
 
   // ref unwrapping
   expectType<number>(unref(arg))
+
+  // ref inner type should be unwrapped
+  const nestedRef = ref({
+    foo: ref(1)
+  })
+  expectType<Ref<{ foo: number }>>(nestedRef)
 }
 
 foo(1)
index 7f49691cb878f8b1b3ad28bb1621b2dd5f443fbc..c532bc2b7ea3382b904382e06d2520cafc269a52 100644 (file)
@@ -52,3 +52,13 @@ watch(
   },
   { immediate: true }
 )
+
+// should provide correct ref.value inner type to callbacks
+const nestedRefSource = ref({
+  foo: ref(1)
+})
+
+watch(nestedRefSource, (v, ov) => {
+  expectType<{ foo: number }>(v)
+  expectType<{ foo: number }>(ov)
+})