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 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;
I found the example above confusing. I am using React and JSX so I think it complicated the scenario.
I got clarification from TypeScript Deep Dive, which states for arrow generics:
Workaround: Use extends on the generic parameter to hint the compiler that it's a generic, this came from a simpler example that helped me.
const identity = < T extends {} >(arg: T): T => { return arg; }
The language specification says on p.64f
A construct of the form < T > ( ... ) => { ... } could be parsed as an arrow function expression with a type parameter or a type assertion applied to an arrow function with no type parameter. It is resolved as the former[..]
example:
// helper function needed because Backbone-couchdb's sync does not return a jqxhr
let fetched = <
R extends Backbone.Collection<any> >(c:R) => {
return new Promise(function (fulfill, reject) {
c.fetch({reset: true, success: fulfill, error: reject})
});
};
I to use this type of declaration:
const identity: { <T>(arg: T): T } = (arg) => arg;
It allows defining additional props to your function if you ever need to and in some cases, it helps keeping the function body cleaner from the generic definition.
If you don't need the additional props (namespace sort of thing), it can be simplified to:
const identity: <T>(arg: T) => T = (arg) => arg;