Function fromGuard

  • Turn a type guard into a validator

    Type Parameters

    • T

      The validated type

    Parameters

    • guard: Guard<T>

      The guard to convert

    • reason: string

      The failure explanation if invalid

    Returns Validator<T>

    Example

    const guard = (input): input is number =>
    typeof input === "number" && !isNaN(input);

    const isNumber = fromGuard(guard, "Not a number");

    isNumber(1) >>
    {
    valid: true,
    parsed: 1,
    };