Ok, I have the following scenario:
type IHashFn = (arg: string) => number;
const hash: IHashFn = (arg) => {
return 42;
}
So far, so good. Now I want the function to be generic.
const hash: <T> (arg: T) => number = (arg) => {
return 42;
}
That works. But this doesn't:
type IHashFn<T> = (arg: T) => number;
const hash: <T> IHashFn<T> = (arg) => {
return 42;
}
I found no way to calm down the TS compiler. Using interfaces instead of type aliases doesn't work either.
Note: I dont't want hash
to be an implementation for IHashFn<string>
but generic as well.
Is there a way to declare generic function types or interfaces in TypeScript at all?
You aren't really doing anything with your generic parameter.
But it sounds like you want a generic function, not a generic type alias. So leave it on the function, and off of the type alias.
type IHashFn = <T>(arg: T) => number;
const hash: IHashFn = (arg) => {
return 42;
}