Assert

The Assert module provides a variety of assertion functions that test a condition or variable and throw an assertion error if the assertion fails.

Where possible, the assertion functions use the asserts keyword so the TypeScript compiler can narrow the types after the assertion where applicable.

Note that assertions are runtime checks, not compile-time checks. You should always prefer compile-time checks and only use assertions when you cannot use compile-time checks, for instance because your project is not using TypeScript or because a value in your code originates from an external API which does not use type-checking (such as a REST end point).

By default, every assertion message is prefixed with “Assertion failed”. You can disable this behavior as well as change the stringification rules for passed values using Assert.configure().

configure()

Allows you to configure various behavior in regards to the assertion messages.

that()

Asserts that an arbitrary condition is true by throwing an assertion error if it isn’t.

empty()

Asserts that an array-like or iterable has no elements and throws an assertion error if it does.

equals()

Asserts that a value equals another and throws an assertion error if it doesn’t.

falsy()

Asserts that a value is falsy and throws an assertion error if it isn’t.

instanceOf()

Asserts that a value has a certain prototype in its prototype chain.

notEmpty()

Asserts that an array-like or iterable isn’t empty and throws an assertion error if it is.

notEquals()

Asserts that a value does not equal another and throws an assertion error if it does.

notNull()

Asserts that a value isn’t null or undefined and throws an assertion error if it is.

truthy()

Asserts that a value is truthy and throws an assertion error if it isn’t.

type()

Asserts that a value has a certain type.

every()

Asserts that every element of an array passes an arbitrary assertion.