Skip to content

Commit 4ca4666

Browse files
authored
fix(toRef): ref created from union typed prop can't be used in watch (#3048)
1 parent 6d5b623 commit 4ca4666

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

packages/reactivity/src/ref.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export interface Ref<T = any> {
2020
_shallow?: boolean
2121
}
2222

23-
export type ToRef<T> = T extends Ref ? T : Ref<UnwrapRef<T>>
23+
export type ToRef<T> = [T] extends [Ref] ? T : Ref<UnwrapRef<T>>
2424
export type ToRefs<T = any> = {
2525
// #2687: somehow using ToRef<T[K]> here turns the resulting type into
2626
// a union of multiple Ref<*> types instead of a single Ref<* | *> type.

test-dts/ref.test-d.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import {
99
proxyRefs,
1010
toRef,
1111
toRefs,
12-
ToRefs
12+
ToRefs,
13+
watch
1314
} from './index'
1415

1516
function plainType(arg: number | Ref<number>) {
@@ -165,6 +166,14 @@ const obj = {
165166
expectType<Ref<number>>(toRef(obj, 'a'))
166167
expectType<Ref<number>>(toRef(obj, 'b'))
167168

169+
const objWithUnionProp: { a: string | number } = {
170+
a: 1
171+
}
172+
173+
watch(toRef(objWithUnionProp, 'a'), value => {
174+
expectType<string | number>(value)
175+
})
176+
168177
// toRefs
169178
const objRefs = toRefs(obj)
170179
expectType<{

0 commit comments

Comments
 (0)