Currying with default parameters in typescript using lodash

Viewed 199

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?
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.

Related