fix(reactivity): ensure `unref` correctly resolves type for `ShallowRef` (#11360)

close #11356
This commit is contained in:
Tycho 2024-07-17 10:09:57 +08:00 committed by GitHub
parent 3ee7b4c7b1
commit a509e30f05
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 8 additions and 2 deletions

View File

@ -452,3 +452,7 @@ describe('toRef <-> toValue', () => {
), ),
) )
}) })
// unref
declare const text: ShallowRef<string> | ComputedRef<string> | MaybeRef<string>
expectType<string>(unref(text))

View File

@ -235,7 +235,7 @@ export type MaybeRefOrGetter<T = any> = MaybeRef<T> | (() => T)
* @param ref - Ref or plain value to be converted into the plain value. * @param ref - Ref or plain value to be converted into the plain value.
* @see {@link https://vuejs.org/api/reactivity-utilities.html#unref} * @see {@link https://vuejs.org/api/reactivity-utilities.html#unref}
*/ */
export function unref<T>(ref: MaybeRef<T> | ComputedRef<T>): T { export function unref<T>(ref: MaybeRef<T> | ComputedRef<T> | ShallowRef<T>): T {
return isRef(ref) ? ref.value : ref return isRef(ref) ? ref.value : ref
} }
@ -255,7 +255,9 @@ export function unref<T>(ref: MaybeRef<T> | ComputedRef<T>): T {
* @param source - A getter, an existing ref, or a non-function value. * @param source - A getter, an existing ref, or a non-function value.
* @see {@link https://vuejs.org/api/reactivity-utilities.html#tovalue} * @see {@link https://vuejs.org/api/reactivity-utilities.html#tovalue}
*/ */
export function toValue<T>(source: MaybeRefOrGetter<T> | ComputedRef<T>): T { export function toValue<T>(
source: MaybeRefOrGetter<T> | ComputedRef<T> | ShallowRef<T>,
): T {
return isFunction(source) ? source() : unref(source) return isFunction(source) ? source() : unref(source)
} }