Skip to content

type-guards

Contains utility functions for type guards.

FunctionDescription
assertAsserts that a condition is true. Throws if it is not. Use in place of /* v8 ignore *\/ for defensive guards that should never trigger at runtime but would otherwise create uncovered branches.
assertGenericObjectAsserts that a value is a generic object, narrowing its type in place.
assertNeverAsserts at compile time that every case of a discriminated union has been handled. Place at the default branch of a switch over a union or enum. If a new variant is added to the union without a matching case, the call to assertNever fails to compile because value is no longer never. If reached at runtime (e.g. because the value was bypassed via JSON or a type assertion), it throws a descriptive Error.
assertNonNullableAsserts that a value is not null or undefined, narrowing its type in place. Only callable when T includes null or undefined. Passing an already non-nullable type is a compile error.
ensureGenericObjectEnsures that a value is a generic object, returning it with narrowed type.
ensureNonNullableEnsures that a value is not null or undefined and returns it with narrowed type. Only callable when T includes null or undefined. Passing an already non-nullable type is a compile error.
TypeDescription
GenericObjectA type that represents a generic object.