Skip to content

type-guards

Contains utility functions for type guards, and combinators that build a TypeGuard out of others.

The combinators here work on bare type guards. Their api-contract counterparts (arrayGuard, optionalGuard, …) work on an ExpectedTypeGuard, a type guard paired with the text an issue message reports, and their names drop the Type this layer’s names carry.

Interface Description
ObjectTypeGuardOptions Options for objectTypeGuard.
Function Description
arrayTypeGuard Builds a guard accepting an array whose every item the item guard accepts.
assert Asserts 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.
assertGenericObject Asserts that a value is a generic object, narrowing its type in place.
assertNever Asserts 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.
assertNonNullable Asserts 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.
ensureGenericObject Ensures that a value is a generic object, returning it with narrowed type.
ensureNonNullable Ensures 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.
functionTypeGuard Builds a guard accepting any function, narrowed to the signature TFunction. Only that the value is a function is checked: a signature cannot be checked at runtime, so the guard trusts it.
instanceOfTypeGuard Builds a guard accepting an instance of a class, or of a class derived from it.
isBoolean Checks whether a value is a boolean.
isFunction Checks whether a value is a function.
isNull Checks whether a value is null.
isNumber Checks whether a value is a number.
isObject Checks whether a value is an object: not null, and not a primitive or a function.
isString Checks whether a value is a string.
isStringArray Checks whether a value is an array of strings.
isUndefined Checks whether a value is undefined.
literalTypeGuard Builds a guard accepting exactly the listed values.
nullableTypeGuard Widens a guard to also accept null.
objectTypeGuard Builds a guard accepting an object whose every listed property its guard accepts. A property is read with Reflect.get, so an inherited one counts and an absent one reads as undefined, unless ObjectTypeGuardOptions.exactOptionalKeys switches the guard to own keys and exact optional properties.
optionalTypeGuard Widens a guard to also accept undefined, which is what an optional property or parameter reads as when it is left out.
unionTypeGuard Builds a guard accepting what any of the given guards accepts.
Type Description
GenericObject A type that represents a generic object.
GuardedType The type a TypeGuard narrows to. Distributes over a union of guards.
Literal A value literalTypeGuard can compare against.
OptionalKeys The keys of an object type that are optional.
PropertyTypeGuards One type guard per property of an object type, as objectTypeGuard takes them. Every key of T must be listed. The guard of an optional property is typed to narrow to undefined as well, but a guard that refuses undefined still compiles there, so widen it with optionalTypeGuard: an absent property reads as undefined, and a guard refusing it refuses the object.
TypeGuard A function deciding whether a value is a T.