Function isNumberAnd

  • Executes a type guard for the number type prior to testing a condition that requires an input of number type, returning true only if both the type check and condition pass.

    Remarks

    This is especially useful in pipelines where the TS type checker would not detect that a type guard had been applied using other alternatives.

    Example

    import * as G from "flurp/guard";
    import * as L from "flurp/logic";

    // isFinite is a JS built-in. While vanilla JS is tolerant,
    // TS requires its parameter to be numeric.

    const goodIsFinite = G.isNumberAnd(isFinite);
    goodIsFinite(5); // true
    goodIsFinite(5/0); // false
    goodIsFinite("5"); // false

    const badIsFinite = L.both(G.isNumber, isFinite);
    badIsFinite("5"); // TS compiler will not recognize G.isNumber as a type guard.

    Type Parameters

    • T

    Parameters

    • condition: ((x: number) => boolean)
        • (x: number): boolean
        • Parameters

          • x: number

          Returns boolean

    Returns ((x: T) => boolean)

      • (x: T): boolean
      • Parameters

        • x: T

        Returns boolean