Function spread

  • Creates a new function that spreads elements of an array argument into individual arguments for the original function.

    Type Parameters

    • F extends ((...args: any[]) => any)

      A function type with any number of parameters and any return type.

    Parameters

    • func: F

      The function to be transformed. It can be any function with any number of arguments.

    Returns ((argsArr: Parameters<F>) => ReturnType<F>)

    • A new function that takes an array of arguments and returns the result of calling the original function with those arguments.
      • (argsArr): ReturnType<F>
      • Parameters

        • argsArr: Parameters<F>

        Returns ReturnType<F>

    function add(a, b) {
    return a + b;
    }

    const spreadAdd = spread(add);
    console.log(spreadAdd([1, 2])); // Output: 3