Let the Compiler Infer · TypeScript

Ch 2 · The Types You Will Use Every Day — card 1 of 5.

Beginners write far too many type annotations, and the code gets noisy enough that they conclude TypeScript is a burden. It is not. TypeScript works out the type of most things on its own, from the value you assigned. This is called inference, and leaning on it is the difference between TypeScript that feels heavy and TypeScript that feels free.

Look at the pair below. Both lines produce a variable of type number. The second one says so; the first one already knew.

let deposit = 5000;
let balance: number = 5000;

console.log(deposit + balance);

Where you do annotate is at the edges of your code — function parameters, function returns, and the shape of data arriving from outside. Those are the places TypeScript has nothing to infer from, and they are exactly the places bugs cross into your program.