assertNotNull
function assertNotNull<T>(value?, message?): T;Defined in: assert-not-null.ts:37
Return value typed without null and undefined, or throw an
AssertError when it is null or undefined.
Only null and undefined fail: other falsy values (0, "", false,
NaN) are returned unchanged.
Type Parameters
Section titled “Type Parameters”| Type Parameter |
|---|
T |
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
value? |
T | null |
The value to check. |
message? |
string |
The AssertError message. Defaults to AssertError’s default, "unexpected null or undefined". |
Returns
Section titled “Returns”T
value, narrowed to T.
Throws
Section titled “Throws”When value is null or undefined.
Examples
Section titled “Examples”Narrow a nullable lookup
import { assertNotNull } from "@xtandard/lib";
const users = new Map([["ada", { name: "Ada" }]]);
const ada = assertNotNull(users.get("ada"), "user ada is missing");ada.name; // "Ada" (ada: { name: string }, not ... | undefined)
assertNotNull(users.get("bob"), "user bob is missing");// throws AssertError("user bob is missing")Only null and undefined fail
assertNotNull(0); // 0assertNotNull(""); // ""assertNotNull(false); // falseassertNotNull(null); // throws AssertError("unexpected null or undefined")