Function guard

  • Turn a validator into a type guard

    Type Parameters

    • T

      The validated type

    Parameters

    Returns ((input) => input is T)

      • (input): input is T
      • Parameters

        • input: unknown

        Returns input is T

    Remarks

    A validator makes a assertions about the input and returns a typed output. In contrast a guard is able to cast the type of the input for the rest of its block.

    Example

    if (guard(isNumber)(input)) {
    // input is treated as a number in this block
    }