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
TypeScript types exist only at compile time — no trace in runtime JS (type erasure)
Structural typing — compatibility by "structure" not "name"
Runtime validation (API responses) needs separate libraries like Zod/Valibot
Type Narrowing possible via typeof/in/instanceof patterns