how to use the ary function from the lodash library in typescript

To use the ary function from the Lodash library in TypeScript, you should first install the Lodash library and its types. You can do this by running the following command:

index.ts
npm install lodash @types/lodash
33 chars
2 lines

Once installed, you can import the ary function from the lodash module and use it like this:

index.ts
import { ary } from 'lodash';

function myFunc(a: number, b: number, c: number) {
  return a + b + c;
}

const twoArgFunc = ary(myFunc, 2);
console.log(twoArgFunc(1, 2, 3)); // Output: 3
187 chars
9 lines

In the example above, we import the ary function from the lodash module and use it to create a new function that only takes two arguments instead of three. We pass myFunc and 2 as arguments to the ary function, which returns a new function that takes the first two arguments passed to it and invokes myFunc with those arguments.

When we call twoArgFunc(1, 2, 3), only the first two arguments (1 and 2) are passed to myFunc, and the result is 1 + 2 = 3.

gistlibby LogSnag