refactor(types/ref): update `MaybeRef` to include all ref-like types (#11379)
Co-authored-by: Evan You <evan@vuejs.org>
This commit is contained in:
parent
d6af69428e
commit
ba2092981c
|
@ -5,6 +5,7 @@ import {
|
||||||
type Ref,
|
type Ref,
|
||||||
type ShallowRef,
|
type ShallowRef,
|
||||||
type ToRefs,
|
type ToRefs,
|
||||||
|
type WritableComputedRef,
|
||||||
computed,
|
computed,
|
||||||
isRef,
|
isRef,
|
||||||
proxyRefs,
|
proxyRefs,
|
||||||
|
@ -465,8 +466,21 @@ describe('toRef <-> toValue', () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
// unref
|
// unref
|
||||||
declare const text: ShallowRef<string> | ComputedRef<string> | MaybeRef<string>
|
// #8747
|
||||||
expectType<string>(unref(text))
|
declare const unref1: number | Ref<number> | ComputedRef<number>
|
||||||
|
expectType<number>(unref(unref1))
|
||||||
|
|
||||||
|
// #11356
|
||||||
|
declare const unref2:
|
||||||
|
| MaybeRef<string>
|
||||||
|
| ShallowRef<string>
|
||||||
|
| ComputedRef<string>
|
||||||
|
| WritableComputedRef<string>
|
||||||
|
expectType<string>(unref(unref2))
|
||||||
|
|
||||||
|
// toValue
|
||||||
|
expectType<number>(toValue(unref1))
|
||||||
|
expectType<string>(toValue(unref2))
|
||||||
|
|
||||||
// useTemplateRef
|
// useTemplateRef
|
||||||
const tRef = useTemplateRef('foo')
|
const tRef = useTemplateRef('foo')
|
||||||
|
|
|
@ -16,7 +16,7 @@ import {
|
||||||
toRaw,
|
toRaw,
|
||||||
toReactive,
|
toReactive,
|
||||||
} from './reactive'
|
} from './reactive'
|
||||||
import type { ComputedRef } from './computed'
|
import type { ComputedRef, WritableComputedRef } from './computed'
|
||||||
import { ReactiveFlags, TrackOpTypes, TriggerOpTypes } from './constants'
|
import { ReactiveFlags, TrackOpTypes, TriggerOpTypes } from './constants'
|
||||||
import { warn } from './warning'
|
import { warn } from './warning'
|
||||||
|
|
||||||
|
@ -192,8 +192,13 @@ export function triggerRef(ref: Ref) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export type MaybeRef<T = any> = T | Ref<T>
|
export type MaybeRef<T = any> =
|
||||||
export type MaybeRefOrGetter<T = any> = MaybeRef<T> | (() => T)
|
| T
|
||||||
|
| Ref<T>
|
||||||
|
| ShallowRef<T>
|
||||||
|
| WritableComputedRef<T>
|
||||||
|
|
||||||
|
export type MaybeRefOrGetter<T = any> = MaybeRef<T> | ComputedRef<T> | (() => T)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the inner value if the argument is a ref, otherwise return the
|
* Returns the inner value if the argument is a ref, otherwise return the
|
||||||
|
@ -211,7 +216,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> | ShallowRef<T>): T {
|
export function unref<T>(ref: MaybeRef<T> | ComputedRef<T>): T {
|
||||||
return isRef(ref) ? ref.value : ref
|
return isRef(ref) ? ref.value : ref
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -231,9 +236,7 @@ export function unref<T>(ref: MaybeRef<T> | ComputedRef<T> | ShallowRef<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>(
|
export function toValue<T>(source: MaybeRefOrGetter<T>): T {
|
||||||
source: MaybeRefOrGetter<T> | ComputedRef<T> | ShallowRef<T>,
|
|
||||||
): T {
|
|
||||||
return isFunction(source) ? source() : unref(source)
|
return isFunction(source) ? source() : unref(source)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue