The Web FrontendDeeper

Kayavak / The Web Frontend / FE·16 TypeScript / Deeper

Deeper · The Web Frontend

TypeScript

The questions the chapter doesn't stop for: why this exists, the model to keep in your head, what changes when an assumption does, how sure we are of each claim, and what's still open. Read it after the chapter; back to the chapter.

Why does this exist?

By around 2010, teams at large companies were writing JavaScript applications of hundreds of thousands of lines, in a language with no way to say what a function expects. Renaming a field meant searching text and hoping. Google's Closure Compiler used type annotations in comments; Microsoft's answer, led by Anders Hejlsberg (who had designed Turbo Pascal and C#), was TypeScript, released publicly in October 2012: a superset of JavaScript whose types are checked and then erased, so the output is ordinary JavaScript.

Two design choices explain why it won over alternatives like Facebook's Flow. It describes JavaScript as it's actually written, so its type system is structural (shapes, not names) and deliberately unsound in places to fit real code. And the editor experience (autocomplete, rename, go-to-definition) came from the same checker, so developers felt its value before they trusted its errors.

The mental model

Try it yourself first: write your own model in the Mental Model Explorer, then compare.

TypeScript is a checker that reads your JavaScript and a set of claims about it, and complains when they disagree. It never runs; at run time, only the JavaScript is left.

Question Answer
What is it? A static type system and checker layered over JavaScript, plus a compiler that strips the types
Problem it solves Catching mistakes and enabling safe refactoring in large JavaScript codebases
Parts Types and inference, unions and narrowing, generics, structural assignability, declaration files, compiler options
How it works The checker infers types from values and annotations, follows control flow to narrow unions, and reports mismatches
Assumptions The types describe the data truthfully
When they change Data from outside (JSON, APIs, any, type assertions) can violate the types silently: validate at the boundary
Where it fails It's intentionally unsound: some type-correct programs still fail at run time
Related ideas Static vs dynamic typing (Foundations, ch02); type inference in compilers (Foundations, ch10)

What if?

What if TypeScript checked types at run time too?

Reason it through, then open

Every function call would pay for checks, and the output would no longer be plain JavaScript that runs anywhere. It would also need a runtime representation of every type, which erasure avoids. The practical compromise is to check at the boundaries where untrusted data enters (with validation libraries that also produce types) and trust the checker inside.

What if TypeScript's types were nominal, like Java's?

Reason it through, then open

Two interfaces with identical fields wouldn't be interchangeable, so ordinary JavaScript patterns (passing an object literal, duck typing) would need explicit declarations everywhere. Structural typing fits how JavaScript is written. When you do want nominal distinctions (a UserId that isn't any string), branding adds a fake property to make otherwise identical shapes incompatible.

Where the boundary is

Aspect A type assertion (x as T) A type guard (narrowing)
What it does Tells the checker "trust me" Proves the type with a run-time check
Checked at run time No Yes
Wrong when The data isn't really a T: errors appear later, elsewhere Never, if the guard is correct
Use for Rare cases the checker can't follow Anything from outside your code

How sure are we?

Claim How it's known Where from
TypeScript was released in October 2012 Historical Microsoft's announcement
Its type system is unsound by design Convention The TypeScript team's stated design goals: soundness isn't a goal
Types are erased at compile time Convention Except enum, namespace and parameter properties, which emit code
Static types prevent a meaningful share of bugs Active research One study (Gao, Bird and Barr, 2017) estimated about 15% of public JavaScript bugs would have been caught; estimates vary

Still open

Whether JavaScript itself should accept type annotations, which engines would ignore, is under discussion at TC39. Faster checkers are being rewritten in native code (the TypeScript team announced a port to Go in 2025).

What would change your mind? If type errors caught at compile time were mostly ones tests also caught, types' value would rest mainly on tooling and documentation. The research above suggests they catch bugs tests miss.

Recall

Do TypeScript types exist at run time?

No; they're erased, apart from a few constructs like enums.

What's structural typing?

Compatibility by shape: any value with the required properties fits the type, whatever its declared name.

Why validate API data even in TypeScript?

The checker can't see outside data; a wrong response violates the types silently unless checked at the boundary.

What's the difference between `as T` and a type guard?

An assertion is an unchecked claim; a guard proves the type with a run-time check.

Reflect

Be honest with yourself; this stays in your browser. Anything you can't tick yet is what to revisit.