Currying in Typescript

· Björn-Eric's Developer notes


1function a(one) {
2  return function b(two) {
3    return function c(three) {
4      console.log(one, two, three);
5    };
6  };
7}

is the same as

1const a = (one) => {
2  return (two) => {
3    return (three) => {
4      console.log(one, two, three);
5    };
6  };
7};

is the same as

1const a = (one) => (two) => (three) => console.log(one, two, three);

They are all called the same

1a(12)(14)(16); // 12, 14, 16

Partially calling #

Calling the function less times than parameters accepted will return the last function with the two previous params baked in

1const c = a(12)(14); // (c) => console.log(a, b, c)
2c(16); // 12, 14, 16

Home