type erasure
The removal of all TypeScript types when code is compiled, so none of them exist while the JavaScript runs.
In more detail
Because types are erased, they add zero bytes to the output and zero run time cost. It also means you cannot ask at run time whether a value is of an interface type; you must check its actual shape with JavaScript code. Data from outside the program, such as a network request, is never protected by types alone.
Example
let n: number = 1 compiles to let n = 1Related terms
- run time — The moment when a program is actually executing, as opposed to when it is written, compiled or built.
- emitter — The last compiler phase, which writes the JavaScript output and optional declaration files from the checked syntax tree.
- runtime validation — Checking real data with JavaScript code while the program runs, so values from outside are proven correct before they are trusted.
- type-only import — An import written import type { X } or import { type X }, which brings in only types and is always removed from the output.
Taught in
- Why TypeScript: Contracts, Erased Types And Soundness — Production TypeScript
- Verify Your Environment: Running tsc And Bun TypeScript — Production TypeScript
- Why Generics: Functions, Identities And Type Variables — Production TypeScript
- Generic Interfaces, Classes And Type Aliases — Production TypeScript
- ECMAScript Modules, CJS Interop And verbatimModuleSyntax — Production TypeScript
- The Spine Artifact: Generating Typed Models From OpenAPI — Production TypeScript