Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Currying with default parameters in typescript using lodash

I have a function that should have a generic parameter, like this:

async function getAll<T>(model: Model<T>, limit = 10) {
   ....
}

So I decided to call this function using lodash curry:

const specificGetAll = curry(getAll)(model)
specificGetAll(10)

This results in a "This expression is not callable. Type '' has no call signatures."

Two questions arise from this:

  1. Is currying the right thing to do here (e.g. compare with ts decorator)?
  2. What causes this error, and how can it be fixed?
like image 745
Lior Pollak Avatar asked Apr 18 '26 13:04

Lior Pollak


1 Answers

Use

const specificGetAll = curry(getAll, 2)(model)

instead of

const specificGetAll = curry(getAll)(model)

As limit is an optional parameter in the getAll function, getAll.length is 1. Lodash wrongly thinks your function getAll has one argument, so curry returns a promise instead of a function.

As for your second question: currying is a commonly known and widely used programming technique. There should be not any difficulties for other programmers working on your code to understand it. However, depending on the library used, there may be occasional problems with the automatic generation of types for curried functions. In that case, be ready to add some types manually.

like image 137
pawel2101 Avatar answered Apr 21 '26 01:04

pawel2101



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!