From: Evan You Date: Wed, 26 Feb 2020 00:44:06 +0000 (-0500) Subject: fix(types): ref value type unwrapping should happen at creation time X-Git-Tag: v3.0.0-alpha.7~8 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=d4c6957e2d8ac7920a649f3a3576689cd5e1099f;p=thirdparty%2Fvuejs%2Fcore.git fix(types): ref value type unwrapping should happen at creation time --- diff --git a/packages/reactivity/src/computed.ts b/packages/reactivity/src/computed.ts index 1a64bdf2a6..154247a672 100644 --- a/packages/reactivity/src/computed.ts +++ b/packages/reactivity/src/computed.ts @@ -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 extends WritableComputedRef { - readonly value: UnwrapRef + readonly value: T } export interface WritableComputedRef extends Ref { diff --git a/packages/reactivity/src/ref.ts b/packages/reactivity/src/ref.ts index f88631130e..dde3b82b7d 100644 --- a/packages/reactivity/src/ref.ts +++ b/packages/reactivity/src/ref.ts @@ -17,7 +17,7 @@ export interface Ref { // 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 + value: T } const convert = (val: T): T => @@ -28,13 +28,13 @@ export function isRef(r: any): r is Ref { return r ? r._isRef === true : false } -export function ref(value: T): T extends Ref ? T : Ref +export function ref(value: T): T extends Ref ? T : Ref> export function ref(): Ref export function ref(value?: unknown) { return createRef(value) } -export function shallowRef(value: T): T extends Ref ? T : Ref +export function shallowRef(value: T): T extends Ref ? T : Ref> export function shallowRef(): Ref export function shallowRef(value?: unknown) { return createRef(value, true) diff --git a/test-dts/ref.test-d.ts b/test-dts/ref.test-d.ts index e1323fef1f..1ae7c355a4 100644 --- a/test-dts/ref.test-d.ts +++ b/test-dts/ref.test-d.ts @@ -13,6 +13,12 @@ function foo(arg: number | Ref) { // ref unwrapping expectType(unref(arg)) + + // ref inner type should be unwrapped + const nestedRef = ref({ + foo: ref(1) + }) + expectType>(nestedRef) } foo(1) diff --git a/test-dts/watch.test-d.ts b/test-dts/watch.test-d.ts index 7f49691cb8..c532bc2b7e 100644 --- a/test-dts/watch.test-d.ts +++ b/test-dts/watch.test-d.ts @@ -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) +})