Skip to content

Commit 5ac7030

Browse files
committed
fix(types): handle ToRef<any>
fix #5188
1 parent 6cfd72e commit 5ac7030

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

packages/reactivity/src/ref.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { isTracking, trackEffects, triggerEffects } from './effect'
22
import { TrackOpTypes, TriggerOpTypes } from './operations'
3-
import { isArray, hasChanged } from '@vue/shared'
3+
import { isArray, hasChanged, IfAny } from '@vue/shared'
44
import { isProxy, toRaw, isReactive, toReactive } from './reactive'
55
import type { ShallowReactiveMarker } from './reactive'
66
import { CollectionTypes } from './collectionHandlers'
@@ -222,7 +222,7 @@ class ObjectRefImpl<T extends object, K extends keyof T> {
222222
}
223223
}
224224

225-
export type ToRef<T> = [T] extends [Ref] ? T : Ref<T>
225+
export type ToRef<T> = IfAny<T, Ref<T>, [T] extends [Ref] ? T : Ref<T>>
226226

227227
export function toRef<T extends object, K extends keyof T>(
228228
object: T,

test-dts/ref.test-d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,11 @@ expectType<Ref<string>>(p2.obj.k)
222222

223223
expectType<Ref<number>>(toRefResult.value.b)
224224
expectType<Ref<number>>(toRefsResult.a.value.b)
225+
226+
// #5188
227+
const props = { foo: 1 } as { foo: any }
228+
const { foo } = toRefs(props)
229+
expectType<Ref<any>>(foo)
225230
}
226231

227232
// toRef default value

0 commit comments

Comments
 (0)