diff --git a/packages/reactivity/src/ref.ts b/packages/reactivity/src/ref.ts index 6b8d541819d..1be1c4de571 100644 --- a/packages/reactivity/src/ref.ts +++ b/packages/reactivity/src/ref.ts @@ -384,6 +384,8 @@ class GetterRefImpl { export type ToRef = IfAny, [T] extends [Ref] ? T : Ref> +type SafeShallowRef = T | ShallowRef + /** * Used to normalize values / refs / getters into refs. * @@ -427,12 +429,15 @@ export type ToRef = IfAny, [T] extends [Ref] ? T : Ref> * @param [key] - (optional) Name of the property in the reactive object. * @see {@link https://vuejs.org/api/reactivity-utilities.html#toref} */ -export function toRef( - value: T, -): T extends () => infer R +export function toRef(value: T): T extends () => infer R ? Readonly> : T extends Ref - ? T + ? // #12986 Workaround for TS <=5.3: Avoids type errors when T is a union + // containing `ShallowRef`, without raising the minimum TS version + // requirement. + T extends { [ShallowRefMarker]?: true } + ? SafeShallowRef + : T : Ref> export function toRef( object: T,