strong-curry
is a tiny package built on the top of just-curry-it
that provides a strongly-typed implementation of function currying.
The whole implementation included in this package comes from the just-curry-it
; this project only aims to provide precise typings for the currying process.
You can install the strong-curry
package using npm
:
$ npm i strong-curry --save
Alternatively, you can also use yarn
:
$ yarn add strong-curry
import { curry } from "strong-curry";
const multiplyBy = curry((a: number, b: number) => a * b);
const double = multiplyBy(2);
const quadruple = multiplyBy(4);
double(5); // 10
quadruple(3); // 12
Functions that take a variable number of arguments require an explicitly stated arity when currying; failing to do so will leave the function uncurried:
import { curry } from "strong-curry";
const sum = (...values: Array<number>) => values.reduce((acc, val) => acc + val, 0);
const sumOf3 = curry(sum, 3);
sumOf3(1, 2, 3); // 6
sumOf3(1)(2, 3); // 6
sumOf3(1, 2)(3); // 6
sumOf3(1)(2)(3); // 6
curry(sum)(1, 2, 3); // 6
curry(sum)(1, 2)(3); // TS2349: This expression is not callable. [...]
Curried<F, N>
type represents a curried function and is effectively a return type of the curry
function.
Because Curried<F, N>
type is idempotent, i.e., Curried<F, N> ≡ Curried<Curried<F, N>, N>
, you can mindlessly use it as a function parameter without worrying about it being applied multiple times:
import { curry, Curried } from "strong-curry";
type BinaryOperator = (a: number, b: number) => number;
const add = curry<BinaryOperator>((a, b) => a + b);
// Some arbitrary function
const compute = (func: Curried<BinaryOperator>): number => func(func(2, 4))(6);
console.log(compute(add)); // OK
console.log(compute(curry(add))); // OK
console.log(compute(curry(curry(add)))); // OK
Like many complex generic-based typings, this package works by defining multiple overloads to provide a type-safe behavior. Because of that, there is a maximum of supported parameters (namely 10). Providing a function with 11 or more parameters will yield the type of the uncurried version.