assert

View sourceassert<T>(test: T, message: string): test is NonNullable<T>

Checks that test is not null or undefined.

All other values, including false, 0, and an empty string, pass this assertion. A successful call returns undefined; a failed call throws via throws and never returns.

Examples

const value = getValue()
assert(value, 'A value is required.')
// `value` is narrowed to a non-nullish value after this call.

Type Parameters

T

Parameters

NameDescriptionDefault
test

Value to check.

—
message

Error message for a nullish value.

—

Throws

When test is null or undefined.

Returns

Returns undefined when test is non-nullish.