TypeScript essentials

Narrowing, generics, discriminated unions, unknown vs any. Types are your API contract — sloppy types leak into every layer above.

About 23 min read3-question knowledge check

The mental model

TypeScript is a **proof checker bolted onto JavaScript** that vanishes before your code runs. Types never execute — they are compile-time claims about runtime values, and tsc is the auditor that checks the claims agree with each other. The analogy that sticks: types are the **building inspection on blueprints**. The inspector catches a staircase leading into a wall before construction; but once the building is up, the blueprints are gone — nothing at runtime enforces them. That gap — *erasure* — explains almost every TypeScript surprise: a value from fetch typed as User is a claim you made, not a fact anyone verified.

Why it exists: in a fullstack codebase, types are the **API contract between layers**. The database row shape, the API response, the component props — each is a boundary where two pieces of code must agree. Without types, that agreement lives in tribal memory and breaks silently: a backend renames userName to username, the frontend keeps reading undefined, and the bug ships because nothing connected the two files. With types flowing from one source of truth, the rename is 47 red squiggles at compile time instead of a production incident.

The second load-bearing idea: TypeScript is structural, not nominal. A type is a shape, not a name — any object with the right properties satisfies it. That's what makes it fit JavaScript's duck-typed reality, and it's why two independently-declared identical interfaces are interchangeable.

The full lesson continues with 15 more sections
  • Deep dive
  • Internal architecture
  • Real-world use cases
  • Best practices
  • Common mistakes
  • Performance
  • Security
  • Production troubleshooting
  • Advantages & disadvantages
  • Alternatives
  • Interview expectations
  • Interview questions
  • Facts to remember
  • Cheat sheet
  • Quick revision