Skip to content

Function: assertNonNullable()

API / @xmachines/play / assertNonNullable

function assertNonNullable<V>(value, name?): NonNullable<V>;

Defined in: packages/play/src/utils.ts:39

Assert that value is neither null nor undefined, and return it typed as NonNullable<V> — combining the guard and the narrowed value in a single expression.

Unlike a non-null assertion (value!), this throws a descriptive error at the point of failure rather than producing undefined-is-not-an-object` crashes somewhere downstream.

Unlike an asserts value is T assertion function, this returns the narrowed value directly, so no separate variable or as cast is required at the call site.

Type Parameters

Type Parameter
V

Parameters

ParameterTypeDescription
valueVThe value to check.
name?stringOptional name included in the error message to identify the missing value (e.g. the variable or injection key name).

Returns

NonNullable<V>

value typed as NonNullable<V>null and undefined removed.

Throws

When value is null or undefined.

Example

import { assertNonNullable } from "@xmachines/play";
// inject() + assertNonNullable in one line — no `!` or intermediate variable:
const actor = assertNonNullable(inject<AuthActor>("actor"), "actor");
// Element lookup:
const el = assertNonNullable(document.getElementById("app"), "#app");