]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(toRef): ref created from union typed prop can't be used in watch (#3048)
author07akioni <07akioni2@gmail.com>
Wed, 3 Feb 2021 18:12:51 +0000 (02:12 +0800)
committerGitHub <noreply@github.com>
Wed, 3 Feb 2021 18:12:51 +0000 (19:12 +0100)
packages/reactivity/src/ref.ts
test-dts/ref.test-d.ts

index ac3016d3e8cd1bf2b67f8acf901d3546737736e4..61ad4665275c42b61ddb67d16a7f04e17cf56805 100644 (file)
@@ -20,7 +20,7 @@ export interface Ref<T = any> {
   _shallow?: boolean
 }
 
-export type ToRef<T> = T extends Ref ? T : Ref<UnwrapRef<T>>
+export type ToRef<T> = [T] extends [Ref] ? T : Ref<UnwrapRef<T>>
 export type ToRefs<T = any> = {
   // #2687: somehow using ToRef<T[K]> here turns the resulting type into
   // a union of multiple Ref<*> types instead of a single Ref<* | *> type.
index 83589f93c93316e27ba8acfa1be37e5b7665b247..a0887ca4babf5f7bc00a9248f32814a5e0652174 100644 (file)
@@ -9,7 +9,8 @@ import {
   proxyRefs,
   toRef,
   toRefs,
-  ToRefs
+  ToRefs,
+  watch
 } from './index'
 
 function plainType(arg: number | Ref<number>) {
@@ -165,6 +166,14 @@ const obj = {
 expectType<Ref<number>>(toRef(obj, 'a'))
 expectType<Ref<number>>(toRef(obj, 'b'))
 
+const objWithUnionProp: { a: string | number } = {
+  a: 1
+}
+
+watch(toRef(objWithUnionProp, 'a'), value => {
+  expectType<string | number>(value)
+})
+
 // toRefs
 const objRefs = toRefs(obj)
 expectType<{