Aug 26, 2017 · 1 min read
Indeed, we need a generic notation. Otherwise, the result (i below) has type any:
const identity = arg => arg;
const i = identity(1);Another way to type the identity function:
type identity = <T> (arg: T) => T;
const identity: identity = arg => arg;
const i = identity(1);
let j = identity(i);Notice the TypeScript compiler accuracy with the code above, in action for instance in VSCode :
- i is typed 1 because it’s a constant, cf.const keyword
- j is typed number because it’s mutable
