TypeScript
TypeScript is JavaScript with a static type layer that is erased at build time. Nothing it checks
exists at runtime, and that single fact explains most of its design: structural typing, assertions
as unchecked promises, any as an escape hatch, and the need for runtime validation at every system
boundary.
The 60-second answer
Section titled “The 60-second answer”“TypeScript is a superset of JavaScript that adds a structural type system and compiles to plain
JavaScript by erasing the types. It catches whole classes of bugs before runtime — typos, wrong
argument counts, unhandled null, non-exhaustive switches — and it powers editor tooling like
go-to-definition and rename. Because types are erased, they cannot validate data coming from
outside: an interface describing an API response is a claim, not a check. Types are a
compile-time contract; runtime validation still needs a schema library.”
What it adds
Section titled “What it adds”| Feature | Benefit | Cost |
|---|---|---|
| Static types | Errors at compile time, self-documenting code | Annotation noise, build step |
| Structural typing | Types match by shape, so you are not tied to class hierarchies | Accidental compatibility |
| Generics | Reusable, type-safe containers and functions | Complexity when overused |
| Narrowing | Refine a union with typeof, in, discriminants |
Requires well-designed unions |
| Editor integration | Autocomplete, refactors, inline docs | Depends on accurate types |
The build step is cheap: tsc type-checks and a bundler strips types. Bundlers do not type-check,
so CI still needs tsc --noEmit.
Structural, not nominal
Section titled “Structural, not nominal”TypeScript compares shapes, not names.
interface Point { x: number; y: number }const length = (p: Point) => Math.hypot(p.x, p.y);
length({ x: 3, y: 4 }); // ok — the literal has the right shapeconst v = { x: 1, y: 2, z: 3 };length(v); // ok — extra properties are allowed for variableslength({ x: 1, y: 2, z: 3 }); // error — excess property check on object literalsThat last line is the excess property check: a freshness rule that only applies to object literals, designed to catch typos. It explains why a variable with extra keys is assignable but the same literal is not.
Where types go away
Section titled “Where types go away”type User = { id: string; name: string };function greet(user: User): string { return `Hello, ${user.name}`; }compiles to exactly the same function with no annotations. So you cannot typeof a type or iterate
its keys at runtime; enum is the exception, because it emits an object, which is one reason many
teams prefer union types. import type makes the erasure explicit and avoids circular runtime
imports.
What interviews actually test
Section titled “What interviews actually test”unknownversusany.anydisables checking and propagates;unknownforces narrowing.- Narrowing. Writing a discriminated union and an exhaustive
switchwithnever. - Generics. Constraining a parameter with
extends, inferring from usage, knowing when a generic adds value versus noise. - Utility types.
Partial,Pick,Omit,Record,ReturnType— and writing a mapped type. interfaceversustype. Declaration merging versus unions.- Runtime boundaries. Why an
APIResponseinterface validates nothing.
A worked example: impossible states
Section titled “A worked example: impossible states”type RequestState<T> = | { status: 'idle' } | { status: 'loading' } | { status: 'success'; data: T } | { status: 'error'; error: Error };
function render(state: RequestState<User>) { switch (state.status) { case 'idle': return 'Nothing yet'; case 'loading': return 'Loading...'; case 'success': return state.data.name; // `data` is only reachable here case 'error': return state.error.message; default: { const exhaustive: never = state; // compile error if a case is missed return exhaustive; } }}Three booleans and three optional fields allow eight states, five of them nonsense. The union allows four, all meaningful, and the compiler enforces which fields exist in each.
How this section is organised
Section titled “How this section is organised”- TypeScript Types — primitives, unions,
narrowing, interfaces versus aliases,
unknown, utility types,satisfies. - Generics — constraints, inference,
keyof, mapped and conditional types. - Promises — typing asynchronous code and the mistakes that survive type checking.
The JavaScript semantics underneath are in JavaScript. TypeScript cannot save you from a misunderstanding of closures or the event loop.
Follow-up questions
Section titled “Follow-up questions”- Why does
tscnot catch a wrong type coming fromJSON.parse? - When would you choose an
interfaceover atypealias? - What does
strictenable, and which flag catches the most bugs? - Why is
anyworse thanunknown?
Common traps
Section titled “Common traps”- “TypeScript checks my API responses.” It only describes what you claim they are.
- Using
asto silence an error. You moved the bug from compile time to runtime. - Believing types exist at runtime. They are erased;
instanceofand schemas are the runtime tools. - Turning on
strictlate without fixingnull.strictNullChecksis where the bugs are, and the noisiest migration. - Over-typing. A type harder to read than the code it describes is a net cost.
- Assuming interfaces and types are interchangeable. They differ on unions, declaration merging and mapped types.