diff --git a/packages/dts-test/ref.test-d.ts b/packages/dts-test/ref.test-d.ts index 0d3764fb7..f8f2500aa 100644 --- a/packages/dts-test/ref.test-d.ts +++ b/packages/dts-test/ref.test-d.ts @@ -5,6 +5,7 @@ import { type Ref, type ShallowRef, type ToRefs, + type WritableComputedRef, computed, isRef, proxyRefs, @@ -465,8 +466,21 @@ describe('toRef <-> toValue', () => { }) // unref -declare const text: ShallowRef | ComputedRef | MaybeRef -expectType(unref(text)) +// #8747 +declare const unref1: number | Ref | ComputedRef +expectType(unref(unref1)) + +// #11356 +declare const unref2: + | MaybeRef + | ShallowRef + | ComputedRef + | WritableComputedRef +expectType(unref(unref2)) + +// toValue +expectType(toValue(unref1)) +expectType(toValue(unref2)) // useTemplateRef const tRef = useTemplateRef('foo') diff --git a/packages/reactivity/src/ref.ts b/packages/reactivity/src/ref.ts index ce6fded56..4cb2aa2c3 100644 --- a/packages/reactivity/src/ref.ts +++ b/packages/reactivity/src/ref.ts @@ -16,7 +16,7 @@ import { toRaw, toReactive, } from './reactive' -import type { ComputedRef } from './computed' +import type { ComputedRef, WritableComputedRef } from './computed' import { ReactiveFlags, TrackOpTypes, TriggerOpTypes } from './constants' import { warn } from './warning' @@ -192,8 +192,13 @@ export function triggerRef(ref: Ref) { } } -export type MaybeRef = T | Ref -export type MaybeRefOrGetter = MaybeRef | (() => T) +export type MaybeRef = + | T + | Ref + | ShallowRef + | WritableComputedRef + +export type MaybeRefOrGetter = MaybeRef | ComputedRef | (() => T) /** * Returns the inner value if the argument is a ref, otherwise return the @@ -211,7 +216,7 @@ export type MaybeRefOrGetter = MaybeRef | (() => T) * @param ref - Ref or plain value to be converted into the plain value. * @see {@link https://vuejs.org/api/reactivity-utilities.html#unref} */ -export function unref(ref: MaybeRef | ComputedRef | ShallowRef): T { +export function unref(ref: MaybeRef | ComputedRef): T { return isRef(ref) ? ref.value : ref } @@ -231,9 +236,7 @@ export function unref(ref: MaybeRef | ComputedRef | ShallowRef): T { * @param source - A getter, an existing ref, or a non-function value. * @see {@link https://vuejs.org/api/reactivity-utilities.html#tovalue} */ -export function toValue( - source: MaybeRefOrGetter | ComputedRef | ShallowRef, -): T { +export function toValue(source: MaybeRefOrGetter): T { return isFunction(source) ? source() : unref(source) }