diff --git a/packages/reactivity/src/ref.ts b/packages/reactivity/src/ref.ts index ac3016d3..61ad4665 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 83589f93..a0887ca4 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<{