Function assertIsNullable

Asserts that a value is either null or undefined.

An error with message 'Expected a non-nullable value' if the value is not null or undefined.

const fn = (x?: number | null) => {
assertIsNullable(x); // throws an error if x is not null or undefined
return x; // x has inferred type null | undefined
};
  • Parameters

    • x: unknown

      The value to be asserted.

    Returns asserts x is undefined | null