🏷️

TypeScript Type System — It All Disappears After Compilation

What structural typing is and why it differs from Java's nominal typing

Java: Cat and Dog with different names are incompatible. TypeScript: if property and method structure matches, they're compatible.

What Remains After Compilation

: string is gone. TypeScript compiler performs type checking then erases all type info (type erasure). Can't check TypeScript types at runtime with typeof.

When This Is a Problem

TypeScript types don't exist at runtime — can't validate API responses. Need runtime validation libraries (Zod, etc.).

Type Narrowing

To check types at runtime, use JavaScript methods: typeof, in, instanceof. TypeScript compiler recognizes these patterns and narrows types inside branches.

Key Points

1

TypeScript types exist only at compile time — no trace in runtime JS (type erasure)

2

Structural typing — compatibility by "structure" not "name"

3

Runtime validation (API responses) needs separate libraries like Zod/Valibot

4

Type Narrowing possible via typeof/in/instanceof patterns

Use Cases

API response validation — Zod + TypeScript for both compile and runtime coverage Library type definitions — understanding why .d.ts files are needed separately