Erased Types
Type erasure means every type annotation, interface, and generic is removed when TypeScript emits JavaScript. The running program has values, not types. Nothing is allocated for a type.
Detailed Explanation
tsc (or a bundler using the TypeScript API) strips types. These all vanish:
- Annotations (
: User,: number) - Type-only declarations (
type,interface,enumwhenpreserveConstEnumsis off forconst enum) - Generics (
<T>) - Parameter properties' modifiers (the assignments stay)
implements,abstract(the class remains)- Non-null assertions,
as,satisfies(the expression stays)
What remains is JavaScript that must still run. A User interface does not become a constructor. x instanceof User is a syntax error if User is an interface. typeof on a value is the JavaScript typeof, which returns "string" or "object", never "User".
Erasure is why TypeScript has no runtime cost for types, and why it cannot check JSON for you. It is also why some syntax is illegal: you cannot write if (value is User) as a runtime test using the type name. You write a type guard that looks at fields, or you keep a class (a real constructor) and use instanceof.
import type and export type exist so the emit can drop the import entirely. A value import of an interface used only as a type would be a runtime undefined if it were left in. verbatimModuleSyntax / importsNotUsedAsValues make that split explicit.
Enums are the historical exception: a numeric or string enum emits an object. const enum inlines and can emit nothing. Classes emit. Namespaces emit objects. Those are values that happen to have types attached, not types that become values.
Memory-wise, erasure means you should not expect a type to "take space." A branded UserId is a string. A Readonly<User> is the same object. A mapped type is not a Proxy. If you need a runtime descriptor (validation, field lists), you write a value — a schema object, a const-asserted key array — and derive the type from the value (typeof). That is the opposite direction of erasure: start with a value, infer the type, and the value remains in the bundle.
When debugging, look at the .js emit. If a check you thought you wrote is gone, it was a type. Put the check in a function.
Code Examples
1. What you write, and what remains.
// 1. What you write, and what remains.
type User = { id: string };
function idOf(user: User): string {
return user.id;
}
// Emitted idea:
// function idOf(user) { return user.id; }2. import type is dropped from emit.
// 2. import type is dropped from emit.
import type { User } from "./user.js";
import { loadUser } from "./user.js";
export function label(user: User): string {
return loadUser(user.id).then((u) => u.id) as unknown as string;
}3. Derive types from values when you need the value at runtime.
// 3. Derive types from values when you need the value at runtime.
const columns = ["id", "email"] as const;
type Column = (typeof columns)[number];
function pick(row: Record<Column, string>, key: Column): string {
return row[key];
}
console.log(columns); // still exists at runtimeCommon Gotchas
You cannot `instanceof` an interface or type alias
only classes (and built-ins) have a runtime constructor.
`enum` emits; `const enum` may not
consumers that expect a runtime object will see `undefined` after a `const enum` inlining. Prefer a const object plus `typeof` for a runtime map.
Brands, `readonly`, and utilities add no fields
if you need a field at runtime, write a field.
`as User` is gone at runtime
it never was a check. See type assertion and type guards.