fix(types): handle ToRef<any>

fix #5188
This commit is contained in:
Evan You 2022-01-16 15:46:49 +08:00
parent 6cfd72e760
commit 5ac703055f
2 changed files with 7 additions and 2 deletions

View File

@ -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<T extends object, K extends keyof T> {
}
}
export type ToRef<T> = [T] extends [Ref] ? T : Ref<T>
export type ToRef<T> = IfAny<T, Ref<T>, [T] extends [Ref] ? T : Ref<T>>
export function toRef<T extends object, K extends keyof T>(
object: T,

View File

@ -222,6 +222,11 @@ expectType<Ref<string>>(p2.obj.k)
expectType<Ref<number>>(toRefResult.value.b)
expectType<Ref<number>>(toRefsResult.a.value.b)
// #5188
const props = { foo: 1 } as { foo: any }
const { foo } = toRefs(props)
expectType<Ref<any>>(foo)
}
// toRef default value