From 5ac703055fa83cb1e8a173bbd6a4d6c33707a3c3 Mon Sep 17 00:00:00 2001 From: Evan You Date: Sun, 16 Jan 2022 15:46:49 +0800 Subject: [PATCH] fix(types): handle ToRef fix #5188 --- packages/reactivity/src/ref.ts | 4 ++-- test-dts/ref.test-d.ts | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/reactivity/src/ref.ts b/packages/reactivity/src/ref.ts index 55059485d6c..c972676ac42 100644 --- a/packages/reactivity/src/ref.ts +++ b/packages/reactivity/src/ref.ts @@ -1,6 +1,6 @@ import { isTracking, trackEffects, triggerEffects } from './effect' import { TrackOpTypes, TriggerOpTypes } from './operations' -import { isArray, hasChanged } from '@vue/shared' +import { isArray, hasChanged, IfAny } from '@vue/shared' import { isProxy, toRaw, isReactive, toReactive } from './reactive' import type { ShallowReactiveMarker } from './reactive' import { CollectionTypes } from './collectionHandlers' @@ -222,7 +222,7 @@ class ObjectRefImpl { } } -export type ToRef = [T] extends [Ref] ? T : Ref +export type ToRef = IfAny, [T] extends [Ref] ? T : Ref> export function toRef( object: T, diff --git a/test-dts/ref.test-d.ts b/test-dts/ref.test-d.ts index f91d6415f70..e585470d5e4 100644 --- a/test-dts/ref.test-d.ts +++ b/test-dts/ref.test-d.ts @@ -222,6 +222,11 @@ expectType>(p2.obj.k) expectType>(toRefResult.value.b) expectType>(toRefsResult.a.value.b) + + // #5188 + const props = { foo: 1 } as { foo: any } + const { foo } = toRefs(props) + expectType>(foo) } // toRef default value