I'm trying to get better with FP but struggling how to deal with typing generic "curry" functions.
For example, I've written a "Curry" version of reduce:
const reduce = <S, R>(fn: (result: R, obj: S, i?: number, arr?: S[]) => R, init: R) =>
(objects: S[]) =>
objects.reduce(fn, init);
const numToSum: number[] = [1,2,3,5,8,13];
const result = reduce((sum, n) => sum + n, 0)(numToSum);
The problem is that typescript obviously can't know the type of S until you actually call the curry function with numToSum.
It's more obvious when you see it without the curry call:
const sumNumbersFn = reduce((sum, n) => sum + n, 0);
In this case you can fix this by typing the arguments of the function itself with n: number or explicitly setting the generic parameters <number[], number>.
The former seems reasonable, but what I'm running into is that as things get more complex, I'm constantly having to provide generic signature.
I'm wondering if I am missing something, and it is actually possible to get typescript to "infer" the types from the later function calls?
Going back to the first example:
const result = reduce((sum, n) => sum + n, 0)(numToSum);
It seems like the compiler actually SHOULD have all the information needed to infer types.
Maybe my types are just off?
Update
Here's a more concrete/full example of the problem I'm running into