Skip to content

What TypeScript checks, and what it erases

BasicsConcept

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.

user.ts
interface User { id: number; name: string }
const text = '{"id": "42", "name": "Ana"}';
const u = JSON.parse(text) as User;
console.log(u.id.toFixed(2));
Text
TypeError: u.id.toFixed is not a function

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.

shape.ts (you write)
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));
shape.js (tsc emits)
"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.

Compiling area(s: Shape): number
Step 1 of 7
Errors 0
area.ts · tokens of one function
function area(
s
: Shape
)
: number
{
return s.w * s.h;
}
What the checker knows
Empty
  • 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.

check phase

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.

TypeScript features after compiling
You writeAt runtime
interface User {}Nothing
type Id = stringNothing
x: number, (): stringNothing
function f<T>(x: T)Nothing; `T` is gone, the function stays
value as UserJust `value`; no conversion, no check
value satisfies UserJust `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`
keep.ts
enum Size { Small, Large }
class User {
constructor(public name: string) {}
}
keep.js
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.

user.ts
interface User { id: number; name: string }
function isUser(x: unknown): x is User {
return (
typeof x === "object" && x !== null &&
typeof (x as User).id === "number" &&
typeof (x as User).name === "string"
);
}
const data: unknown = JSON.parse(text);
if (!isUser(data)) throw new Error("bad user");
console.log(data.id.toFixed(2)); // safe: checked at runtime

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.

07 Check yourself

Question

tsc reports a type error in app.ts, and you have not set noEmitOnError. What happens to app.js?

Question

What does const u = data as User; turn into in the JavaScript output?

Question

Which of these still exists when the program runs?

08 Practise this

Was this tutorial useful?Your vote tells us what to improve next.
Spotted a mistake, or something unclear?