assertion function

A function whose return type is asserts condition or asserts val is Type, which throws if the check fails and otherwise narrows the caller's code.

In more detail

After assertDefined(orderId, "Order ID is required"), the rest of the function knows orderId is a string, with no if block needed. It must return nothing and throw on failure. This flattens code that would otherwise nest many if statements.

Example

function assertDefined<T>(val: T, msg: string): asserts val is NonNullable<T>

Related terms

Taught in

References