TypeScript
JavaScript with a spellchecker.
▶ the lesson · narrated sketch video · tap for sound
the intro
TypeScript is JavaScript with types. You describe the shape of your data once, and the compiler catches mistakes before the code runs — plus your editor gains autocomplete that actually knows your code. It compiles to plain JavaScript and has become the default for serious frontend work.
the sample
types catch bugs before runtime
type User = {
name: string
age: number
}
const u: User = { name: "Ericko", age: "oops" }
// ~~~~ error: string is not numberfun fact ✦
TypeScript is written in TypeScript — the compiler compiles itself.
the docs
the deep dive
the ideas behind the video — concepts, diagrams, a walkthrough and the traps to avoid
Types are documentation the compiler enforces. Describe the shape of your data once and three things happen: mistakes surface in the editor instead of production, autocomplete starts telling the truth, and refactors stop being acts of faith. The runtime never sees any of it.
types are docs the compiler enforces
key terms
annotation
You label the type explicitly. Do it at function borders, where intent matters most.
let age: number = 30inference
TypeScript figures types out on its own from the initializer. Most locals never need a label.
const n = 1 is numberunion
A type that allows one of several options. String unions replace most enums in modern code.
type Role = "admin" | "user"generic
A type variable: one function, many types, all still checked.
function first<T>(a: T)how it works
- 1Describe the shape
type User = { name: string, age: number }. You have documented the data and armed the checker in one line.
- 2Annotate the borders
Function parameters and return types are where labels pay the most. Inside the body, let inference flow.
- 3Run the check
npx tsc --noEmit, or just read the editor squiggles. The mistake is now yours to fix privately, before deploy.
- 4Compile and ship
The output is plain JavaScript with every type stripped. The safety lived in your workflow, not the runtime.
the code, explained
the second call never reaches production
side by side
any
- turns checking off
- contagious through calls
- autocomplete goes dark
- fine for a five-line hack
unknown
- accepts anything, trusts nothing
- forces a check before use
- right at API boundaries
- the honest any
TypeScript erases at compile time: at runtime there are no types, only data. That JSON from the API is typed User because you said so, not because anyone checked. Validate untrusted input at the boundary — zod or plain checks — and treat every "as" cast as a small lie.
quick answers
Do browsers run TypeScript?
No. tsc or your bundler compiles it to JavaScript first; types are fully erased before anything ships.
Is it worth it for small projects?
Usually yes — the autocomplete alone pays rent. For a one-off 50-line script, plain JS is fine.
Interface or type?
Interfaces can merge and extend; types do unions and mapped types. Modern teams mostly write types. Pick one and stay consistent.
quick hits about TypeScript
short answers · interviews, tips, trivia & hidden gems