The declared type of the value to validate
The value to validate
The predicate to apply to value, declared over its type or over any supertype of it
Optionalmessage: string | ((value: T) => string)Optional custom error message, either a string or a factory computing it from the offending value;
factories are evaluated only if predicate fails, so building expensive messages costs nothing on success; defaults
to a message derived from the predicate function name
The value argument, unchanged
error for throwing where a statement isn't allowed
Validates a value against a predicate and returns it.
Applies the predicate to the value: if it passes, returns the value unchanged; otherwise, throws a
TypeError. When no custom message is provided, derives a descriptive message from the predicate function name: names inisXxxform report the camel case suffix as separate lowercase words (for example, isAsyncIterable produces "expected async iterable"), while other names produce "assertion failed".The predicate may be declared over the type of the value or over any of its supertypes, so Guard functions accepting
unknownare taken as is and the result keeps the declared type of the value, without widening it to the predicate parameter type. Validation doesn't narrow, though: a value declared asunknownis returned asunknown, whatever type the predicate checks for.