Experiments · JavaScript: From Closures to Autonomous Systems · Chapter 4

Type erasure: what reaches the engine

Type erasure in TypeScript

Theme
Try itType erasure: what reaches the engine
Above, the book’s point.ts; below it, what Node 24 runs: the same lines with spaces where the types were. Change the JSON “from the network” and run the code in your browser. On the second tab, code that tsc --strict accepts without a word fails with a TypeError.

Everything is computed in your browser; nothing is sent anywhere.

About this experiment

What is type erasure in TypeScript?

Type erasure means that TypeScript’s types exist only while a program is being checked and are removed before it runs. The compiler, or the type stripping built into Node, deletes interfaces and annotations and leaves plain JavaScript, so at run time nothing knows that a variable was declared a number. TypeScript checks the text of a program, not the values that arrive: the result of JSON.parse has the type any and can be given any declared type without a check. Values from outside the program therefore have to be checked by code that runs, for example a parser written with a library such as zod.

Since version 23, Node runs TypeScript files without compiling them: it cuts out the type declarations and runs what is left. The book’s point.ts declares an interface Point with numeric x and y, a function norm that returns Math.hypot(p.x, p.y), and an object read from the JSON {"x": "3", "y": null}. Node 24 prints 3 string. The program declared x a number and got a string, Math.hypot quietly turned the string into 3 and null into 0, and nobody noticed.

On the first tab, point.ts sits beside the text Node actually runs: the same lines, with spaces where the types were, so every position in the file stays where it was. Edit the JSON from the network or pick another one and press Run. The stripped text runs in your browser’s engine with a real JSON.parse, and a table shows what Point declared, what arrived and what Math.hypot made of it. The second tab is cov.ts: an array of cats passed where an array of animals is expected, a dog pushed into it and meow called on the first cat. Step through it line by line; the last line fails with the message your engine writes.

In the book, Node 24.16.0 runs .ts files directly, with type stripping on by default since 23.6 and 22.18 and no type checking. point.ts prints 3 string. cov.ts passes tsc --strict from TypeScript 5.6.3 without errors, and node cov.ts fails with TypeError: cats[0].meow is not a function.

The chapter’s conclusion is that three parties check types in JavaScript and none of them is responsible for everything. TypeScript reads the text before the program runs and deliberately lets through what would be too awkward to forbid. The engine sees every value but cares about speed, not correctness. Data from outside is left to parsing at the point where it enters, which works only where the programmer has put it.

From the book

JavaScript: From Closures to Autonomous Systems · Chapter 4

Types Checked Later

Next to this experiment in the chapter
TypeScript decides type compatibility by the structure of an object. Any object with numeric x and y fits the description of Point, wherever it came from, and for a language where objects are moulded out of curly braces and changed on the fly, this is very convenient. V8 also thinks about the structure of objects, only in its own way: its shapes are the hidden classes from the first chapter, the order of fields and how they lie in memory. The two systems describe the same thing from different sides and never meet. Everything TypeScript has learned about object shapes is cut out before the code reaches the engine, and V8 works out all over again what the TypeScript compiler knew a second earlier.