Skip to content

asserts

The asserts() function throws when a predicate fails and narrows the input type when it passes.

Interactive example

Syntax

typescript
function asserts<
	GenericInput extends unknown,
	GenericPredicate extends GenericInput
>(
	input: GenericInput,
	predicate: (input: GenericInput) => input is GenericPredicate
): asserts input is GenericPredicate

Parameters

  • input: The value to validate.
  • predicate: A type-guard predicate used to validate and narrow the input.

Return value

Nothing. It throws an AssertsError when the predicate fails.

See also

  • isType - Builds type guards based on runtime checks
  • instanceOf - Type guard using constructors

Released under the MIT license.