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

    Function assert

    Validates a value against a type guard or a predicate.

    • Validates a value against a type guard and returns it at the guarded type.

      Returns the value unchanged if the guard accepts it, narrowed to the guarded type; otherwise, throws a TypeError reporting the expected type, as read from the name of the guard when it is in isXxx form (for example, isAsyncIterable produces "expected value"), or a generic "assertion failed" otherwise.

      Type Parameters

      • T

        The declared type of the value to validate

      • G

        The type guarded by guard

      Parameters

      • value: T

        The value to validate, whatever its declared type

      • guard: (value: T) => value is G

        The Guard 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 guard fails, so building expensive messages costs nothing on success; defaults to a message derived from the guard function name

      Returns G

      The value argument, unchanged, typed as G

      TypeError When guard returns false

    • Validates a value against a predicate and returns it.

      Returns the value unchanged if the predicate accepts it; otherwise, throws a TypeError reporting the expected type, as read from the name of the predicate when it is in isXxx form (for example, isString produces "expected value"), or a generic "assertion failed" otherwise.

      A plain predicate carries no narrowing information, so the result keeps the declared type of the value, whatever the predicate tests 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

      TypeError When predicate returns false