What TypeScript checks, and what it erases
Your types say id is a number, yet at runtime it is the string "42" and nothing warned you. That is because types never reach runtime: here is what the compiler checks, and what it throws away.
01 A type that was not there
An API returns a user as JSON. You tell TypeScript the result is a User, the code compiles without a single error, and then it crashes the first time it runs.
The server sent id as a string. TypeScript never saw the server’s reply: it only saw your promise, as User, and believed it. To see why it could not do better, look at what the compiler actually produces.
02 The compiler has two jobs
tsc does two separate things. First it checks: it works out a type for every expression and reports mismatches. Then it emits: it writes JavaScript. The emitted file is your code with the type syntax removed. The checker’s results do not go into it.
interface Shape {w: number;h: number;}function area(s: Shape): number {return s.w * s.h;}const box = { w: 3, h: 4 } as Shape;console.log(area(box));
"use strict";function area(s) {return s.w * s.h;}const box = { w: 3, h: 4 };console.log(area(box));
The interface is gone entirely. So are : Shape, : number and as Shape. This is real tsc 5.9 output with --target es2022. Even if your code has type errors, tsc still emits this same JavaScript by default; the errors stop nothing unless you set noEmitOnError.
03 Check, then erase
Here is the same function going through both jobs. Watch what the checker learns, and what happens to that knowledge when the emitter runs.
- being read
- type looked up
- fact recorded
- check passed
- erased
- not reached yet
The compiler has the source of area. Nothing has been checked or emitted yet.
Node.js 24 can run .ts files directly (node shape.ts). It does this by type stripping: it blanks out the type syntax and runs the rest. It does not type-check at all, so node will happily run code that tsc rejects.
04 What survives, and what does not
Most TypeScript syntax is erasable: delete it and valid JavaScript remains. A few features are not. They generate code, so they do exist at runtime.
| You write | At runtime |
|---|---|
| interface User {} | Nothing |
| type Id = string | Nothing |
| x: number, (): string | Nothing |
| function f<T>(x: T) | Nothing; `T` is gone, the function stays |
| value as User | Just `value`; no conversion, no check |
| value satisfies User | Just `value` |
| class User {} | A real JavaScript class |
| enum Size { Small, Large } | An object: `Size.Large` is `1`, `Size[1]` is `"Large"` |
| constructor(public name: string) | An assignment `this.name = name` |
enum Size { Small, Large }class User {constructor(public name: string) {}}
var Size;(function (Size) {Size[Size["Small"] = 0] = "Small";Size[Size["Large"] = 1] = "Large";})(Size || (Size = {}));class User {name;constructor(name) {this.name = name;}}
Because these need real code generation, Node’s type stripping refuses them: running keep.ts with node fails with TypeScript enum is not supported in strip-only mode. TypeScript 5.8 added the erasableSyntaxOnly option, which reports them at compile time instead.
05 Check data where it enters
Inside your program, types are trustworthy because the checker saw every assignment. At the edges, where data arrives from JSON, localStorage, a form or a file, it saw nothing. That data is unknown until runtime code proves otherwise.
The typeof checks survive compiling because they are JavaScript. The x is User part is erased, but before it goes it tells the checker that a true result means data is a User. Libraries such as Zod do the same job: you describe the shape once as a runtime value and derive the type from it.
as User is a promise to the compiler, not a check. It emits nothing. Use it only when you know more than the checker, never to turn outside data into a trusted type.
06 When erasure bites
if (x instanceof User)instanceof on an interfaceinstanceof needs a constructor at runtime, and an interface has none. tsc stops you: 'User' only refers to a type, but is being used as a value here. Check the fields instead, or make User a class.function make<T>(): T { return new T(); }Using a generic at runtimeT does not exist once compiled, so you cannot create one, test against it or read its name. Pass a real value instead, such as a constructor or a parser.const cfg = JSON.parse(raw) as Config;Casting outside dataCompiles, emits a plain JSON.parse, and lets a wrong shape travel deep into your program before it fails somewhere unrelated. Validate it where it arrives.node app.tsRunning is not checkingNode, tsx, esbuild and Babel all strip types without checking them. Run tsc --noEmit in your build or CI, or type errors will never stop anything.