I have the following scenario Playground
I want to curry twice basically and make sure TS infers the types:
type Picks = Record<
string,
{
a: () => any
b: () => any
}
>
const picks = {
first: {
a: () => 'something',
b: () => 'something else'
},
second: {
a: () => 'something',
b: () => 'something else'
}
}
const pickSomething = <T extends Picks>(picks: T) =>
<K extends keyof T>(key: K): ReturnType<T[K]['b']> => {
const { a, b } = picks[key]
return a() + b()
}
const works = pickSomething(picks)
// but i would like to do a but more currying:
const someOtherPickFunction =
<T extends ReturnType<typeof pickSomething>>(fn: T) =>
<K extends Parameters<T>[0]>(key: K) => {
const result = fn(key) as ReturnType<typeof fn<K>> // i would like to do ReturnType<T<K>> but that is not supported. is there a way to get around it?
return result
}
// here the result is any.
const mainFn = someOtherPickFunction(pickSomething(picks))('first')
It would work I guess if T<K>
was possible to do. Is there a way to go around this?