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 number

fun 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

your codetscplain JS
the check happens before your users ever meet the bug

key terms

annotation

You label the type explicitly. Do it at function borders, where intent matters most.

let age: number = 30

inference

TypeScript figures types out on its own from the initializer. Most locals never need a label.

const n = 1 is number

union

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

  1. 1
    Describe the shape

    type User = { name: string, age: number }. You have documented the data and armed the checker in one line.

  2. 2
    Annotate the borders

    Function parameters and return types are where labels pay the most. Inside the body, let inference flow.

  3. 3
    Run the check

    npx tsc --noEmit, or just read the editor squiggles. The mistake is now yours to fix privately, before deploy.

  4. 4
    Compile 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

1type User = { name: string; age: number }
2function greet(u: User): string {
the contract: a User in, a string out
3 return "Hi " + u.name
4}
5greet({ name: "Ada", age: 36 })
6greet({ name: "Ada" })
red squiggle at write time: age is missing

side by side

any

  • turns checking off
  • contagious through calls
  • autocomplete goes dark
  • fine for a five-line hack
VS

unknown

  • accepts anything, trusts nothing
  • forces a check before use
  • right at API boundaries
  • the honest any
🪤 gotcha

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

🎤 interview

Interface or type?watch ↗

🎤 interview

Any vs unknown?watch ↗

🎤 interview

Enums or union types?watch ↗

📚 tutorial

What is a generic?watch ↗

🔧 useful

Rewriting types by hand?watch ↗

💎 deep

What does satisfies do?watch ↗

🔧 useful

Type everything?watch ↗

🧠 trivia

What is TypeScript written in?watch ↗