Review: Type System and Type Errors
Revisit and reinforce the foundations of TypeScript’s type system—its rules and the common pitfalls it helps us catch.
We’ve covered a lot of ground—and this is where it all starts to click.
So far, we’ve built a good understanding of how TypeScript thinks: how it infers types, when it needs our help, and how it catches the bugs we’d rather not meet in production. This review is our moment to step back, connect the dots, and lock it in.
It’s about internalization. We want to make the type system feel like second nature.
The engine behind the scenes: Inference
TypeScript doesn’t need us to annotate everything. Most of the time, it can figure things out just fine:
let score = 100; // inferred as numberconst theme = "dark"; // inferred as the literal "dark"
But inference has limits. If we declare a variable without an initializer or with something vague like null
—TypeScript has no clue what we mean. That’s when it falls back to any
—the type that disables all checking and turns TypeScript into plain JavaScript.
To keep our code safe:
Initialize variables with meaningful values.
Use
const
...