Romain Deneau
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

)

    Romain Deneau

    Written by

    C#/TypeScript full-stack developer | Software craftsman | UX / Web Design | Working @ maf.fr / soat.fr • Paris, France

    Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
    Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
    Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade