fn
isDefined
v0.0.14testeddemoType-guard that checks whether a ref's (or plain value's) current
value is neither null nor undefined, narrowing the source to its
NonNullable form. Unwraps refs with a single unref — no extra reactivity
or watchers are created, so it is a cheap synchronous check that is fully
SSR-safe. For a reactive guard that tracks changes, use useIsDefined.
Examples
ts
const user = ref<User | null>(null);
if (isDefined(user)) {
// `user` is now narrowed to `Ref<User>`
console.log(user.value.name);
}ts
const value = isDefined(maybeNumber) ? maybeNumber.value : 0;Demo
Loading demo…
Signatures
ts
export function isDefined<T>(value: ComputedRef<T>): value is ComputedRef<NonNullable<T>>;ts
export function isDefined<T>(value: Ref<T>): value is Ref<NonNullable<T>>;ts
export function isDefined<T>(value: T): value is NonNullable<T>;Type Parameters
TParameters
| Parameter | Type | Description |
|---|---|---|
value | Ref<T> | T | The ref or value to test |
Returns
IsDefinedReturntrue when the unwrapped value is non-nullish