Function partial

  • Partially applies arguments to a function, fixing some parameters from the left.

    Type Parameters

    • T extends unknown[]

      The tuple type of the fixed arguments.

    • U extends unknown[]

      The tuple type of the remaining arguments.

    • R

      The return type of the function.

    Parameters

    • fn: ((...args: [...T[], ...U[]]) => R)

      The function to partially apply.

        • (...args): R
        • Parameters

          • Rest...args: [...T[], ...U[]]

          Returns R

    • Rest...presetArgs: T

      The arguments to fix from the left.

    Returns ((...remainingArgs: U) => R)

    • A new function that takes only the remaining arguments.
      • (...remainingArgs): R
      • Parameters

        • Rest...remainingArgs: U

        Returns R

    const multiply = (a: number, b: number, c: number) => a * b * c;
    const multiplyBy2 = partial(multiply, 2);
    console.log(multiplyBy2(3, 4)); // Output: 24