@metreeca/core - v0.9.22
    Preparing search index...

    Function assert

    • 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 in isXxx form 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 unknown are 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 as unknown is returned as unknown, whatever type the predicate checks for.

      Type Parameters

      • T

        The declared type of the value to validate

      Parameters

      • value: T

        The value to validate

      • predicate: (value: T) => boolean

        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

      Returns T

      The value argument, unchanged

      When predicate returns false

      error for throwing where a statement isn't allowed