Function curry

  • Converts a function that takes multiple arguments into a curried function.

    Type Parameters

    • T extends unknown[]

      The tuple type of the function's arguments.

    • R

      The return type of the function.

    Parameters

    • fn: ((...args: T) => R)

      The function to curry.

        • (...args): R
        • Parameters

          • Rest...args: T

          Returns R

    Returns Curried<T, R>

    • A curried version of the function.
    const add = (a: number, b: number, c: number) => a + b + c;
    const curriedAdd = curry(add);
    console.log(curriedAdd(1)(2)(3)); // Output: 6
    console.log(curriedAdd(1, 2)(3)); // Output: 6
    console.log(curriedAdd(1)(2, 3)); // Output: 6
    console.log(curriedAdd(1, 2, 3)); // Output: 6