From: 07akioni <07akioni2@gmail.com> Date: Wed, 3 Feb 2021 18:12:51 +0000 (+0800) Subject: fix(toRef): ref created from union typed prop can't be used in watch (#3048) X-Git-Tag: v3.0.6~56 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4ca4666d58ee8025570dc14f1c163bdeac9c6012;p=thirdparty%2Fvuejs%2Fcore.git fix(toRef): ref created from union typed prop can't be used in watch (#3048) --- diff --git a/packages/reactivity/src/ref.ts b/packages/reactivity/src/ref.ts index ac3016d3e8..61ad466527 100644 --- a/packages/reactivity/src/ref.ts +++ b/packages/reactivity/src/ref.ts @@ -20,7 +20,7 @@ export interface Ref { _shallow?: boolean } -export type ToRef = T extends Ref ? T : Ref> +export type ToRef = [T] extends [Ref] ? T : Ref> export type ToRefs = { // #2687: somehow using ToRef here turns the resulting type into // a union of multiple Ref<*> types instead of a single Ref<* | *> type. diff --git a/test-dts/ref.test-d.ts b/test-dts/ref.test-d.ts index 83589f93c9..a0887ca4ba 100644 --- a/test-dts/ref.test-d.ts +++ b/test-dts/ref.test-d.ts @@ -9,7 +9,8 @@ import { proxyRefs, toRef, toRefs, - ToRefs + ToRefs, + watch } from './index' function plainType(arg: number | Ref) { @@ -165,6 +166,14 @@ const obj = { expectType>(toRef(obj, 'a')) expectType>(toRef(obj, 'b')) +const objWithUnionProp: { a: string | number } = { + a: 1 +} + +watch(toRef(objWithUnionProp, 'a'), value => { + expectType(value) +}) + // toRefs const objRefs = toRefs(obj) expectType<{