I'm trying to have a function fun declared in TypeScript that uses its type parameter T as a constraint for what keys can be used as a parameter of another function context.Foo:
interface Context<T> {
Foo(key: keyof T)
}
function fun<T>(provider: (context: Context<T>) => T): number {
return 1
}
fun<{a: number}>(context => {
return {a: context.Foo('a')}
})
So far so good, I can only use 'a' literal as a key parameter of context.Foo in the foo invocation at the bottom, because this invocation has a type parameter that declares T to be {a: number}.
What bothers me is that I actually define the structure of this type inline as a provider function body. I can only return an object that matches {a: number} definition.
What I'd like to achieve is to get rid of the need to explicitly define the T type of fun at invocation time and let type inference figure this type out by itself by the structure of the object I'm returning from the fun's parameter (that is required to be T), like this:
fun(context => {
return {a: context.Foo('a')}
})
Unfortunately, this ends with an error:
TS2345: Argument of type '"a"' is not assignable to parameter of type 'never'.
Type inference for T failed and have fallen back to never which is definitely not useful in this case.
Is it possible to define this function in such a way that explicit setting of fun's T is not needed?
Context.Foo? Is it always the same type or is it generic?infer.string.