Let say I have a function like this(I know it is a nonsense):
const fn = <T>(a: number) => {
return a as unknown as Promise<T>
}
When I separate the type and the function with type alias like this:
type Fn = <T>(a: number) => Promise<T>
const fn: Fn = <T>(a) => {
return a as unknown as Promise<T>
}
I will get the following error:
Parameter 'a' implicitly has an 'any' type.
Why is that? How can I use generic when I type with type alias?
CodePudding user response:
When you're working with generic lambdas, TypeScript (as far as I'm concerned) won't allow you implicit 'any' types in the argument list. A solution (workaround) would be to just add the type:
type Fn = <T>(a: number) => Promise<T>
const fn: Fn = <T>(a: number) => {
return a as unknown as Promise<T>
}
So far I'm not aware of any other solution that would enable you to use lambdas with inferred argument types.