Skip to content

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.

“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.”

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.

TypeScript compares shapes, not names.

structural.ts
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 shape
const v = { x: 1, y: 2, z: 3 };
length(v); // ok — extra properties are allowed for variables
length({ x: 1, y: 2, z: 3 }); // error — excess property check on object literals

That 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.

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.

  1. unknown versus any. any disables checking and propagates; unknown forces narrowing.
  2. Narrowing. Writing a discriminated union and an exhaustive switch with never.
  3. Generics. Constraining a parameter with extends, inferring from usage, knowing when a generic adds value versus noise.
  4. Utility types. Partial, Pick, Omit, Record, ReturnType — and writing a mapped type.
  5. interface versus type. Declaration merging versus unions.
  6. Runtime boundaries. Why an APIResponse interface validates nothing.
request-state.ts
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.

  • 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.

  1. Why does tsc not catch a wrong type coming from JSON.parse?
  2. When would you choose an interface over a type alias?
  3. What does strict enable, and which flag catches the most bugs?
  4. Why is any worse than unknown?
  • “TypeScript checks my API responses.” It only describes what you claim they are.
  • Using as to silence an error. You moved the bug from compile time to runtime.
  • Believing types exist at runtime. They are erased; instanceof and schemas are the runtime tools.
  • Turning on strict late without fixing null. strictNullChecks is 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.