What I'm trying to achieve is something like this:
function genericFunc<T>(arg: T): T {
return arg;
}
//this line won't work, but it's the essence of what I need
var stringFunc = genericFunc<string>;
stringFunc('myStr')
Is it possible to create an instance of a generic function with a specified type?
The only way I know is to create an interface -
interface GenericFnInterface<T> {
(arg: T): T;
}
let stringFunc : GenericFnInterface<string> = genericFunc;
but I would like to avoid creating too many interfaces. Is there a shorter way to achieve the same
_
UPD Another way I found is
var stringFunc: <T extends string>(arg:T) => T = genericFun;
but it's still not exactly perfect, as it creates a lot of clutter with more complex types.