The typescript handbook currently has nothing on arrow functions. Normal functions
can be generically typed with this syntax:
example:
function identity<T>(arg: T): T {
return arg;
}
What is the syntax for arrow functions?
The typescript handbook currently has nothing on arrow functions. Normal functions
can be generically typed with this syntax:
example:
function identity<T>(arg: T): T {
return arg;
}
What is the syntax for arrow functions?
Per @Thomas comment, in newer TS compilers, we can simply do:
const foo = <T,>(x: T) => x;
The full example explaining the syntax referenced by Robin… brought it home for me:
Something like the following works fine:
function foo<T>(x: T): T { return x; }
However using an arrow generic function will not:
const foo = <T>(x: T) => x; // ERROR : unclosed `T` tag
Workaround: Use extends on the generic parameter to hint the compiler
that it’s a generic, e.g.:
const foo = <T extends unknown>(x: T) => x;