Function CanApply

  • A functor that safely applies transformations to a value, preventing errors while allowing type changes.

    Type Parameters

    • T

    Parameters

    • value: T

      The initial value.

    Returns {
        getValue: (() => T);
        map: (<U>(fn: ((x: T) => U)) => any);
    }

    A CanApply<T> instance.

    • getValue: (() => T)
        • (): T
        • Returns T

    • map: (<U>(fn: ((x: T) => U)) => any)
        • <U>(fn): any
        • Type Parameters

          • U

          Parameters

          • fn: ((x: T) => U)
              • (x): U
              • Parameters

                Returns U

          Returns any

    const result = CanApply(5)
    .map(x => x * 2) // 10
    .map(x => x.toString()) // "10"
    .getValue();
    console.log(result); // "10"