typescriptgenericsramda.jscurrying

Curry generic function with Ramda and Typescript


I have a generic function:

function hello<T>(n: number, s: string, thing: T): Array<T> {
  return [thing]
}

const result = hello(1, 'string arg', 'generic arg')

result has type string[] which is expected.

However if I curry it:

function hello<T>(n: number, s: string, thing: T): Array<T> {
  return [thing]
}

const fun1 = curry(hello)(1, 'string arg')

const result = fun1('generic arg')

result now has type unknown[].

How can I curry a generic function in Ramda whilst maintaining the type? I'm using Ramda 0.27.1


Solution

  • The signature of R.curry with 3 parameters is:

    curry<T1, T2, T3, TResult>(fn: (a: T1, b: T2, c: T3) => TResult): CurriedFunction3<T1, T2, T3, TResult>;
    

    As you can see, you'll need to manually type the curried function (codesandbox):

    function hello<T>(n: number, s: string, thing: T): Array<T> {
      return [thing];
    }
    
    const fun1 = curry<number, string, string, string[]>(hello)(1, 'string arg');
    
    const result = fun1('generic arg');
    
    console.log(result);