The compiler and runtime

Understand type erasure

Know which TypeScript features disappear and why a type cannot be inspected like a JavaScript value.

TypeScript removes type-only syntax when it emits JavaScript.

type User = { name: string }

function greet(user: User) {
  return `Hello ${user.name}`
}

Compile that and look at the output:

function greet(user) {
  return `Hello ${user.name}`
}

The emitted JavaScript has the function, but no User alias or parameter annotation. The alias did not become some runtime registry entry. It is gone.

This is type erasure. Interfaces, aliases, annotations, and type-only imports exist for checking and editor tools. JavaScript cannot inspect them later, because by the time the code runs, they no longer exist anywhere.

Types are not values

That is why this cannot work:

value instanceof User

The compiler rejects it with a very literal message:

'User' only refers to a type, but is being used as a value here.

User is not a runtime value. instanceof is a JavaScript operator that needs a constructor function on its right side, and after erasure there is nothing there. The same applies to ideas like typeof value === User or looping over a type’s properties — the runtime has no types to consult.

When you need a runtime decision, use something that survives compilation. Use a real class with instanceof, check object properties, or parse the value with a schema. A class works because a class declaration is both a type and a value: it emits a constructor function that exists at runtime.

What erasure buys and costs

Erasure also means types do not add runtime cost or automatically validate data. The emitted program remains ordinary JavaScript, exactly as fast as the untyped version, with no library shipped to your users.

The cost is the flip side: annotating a network response as User changes nothing about the actual bytes that arrive. The annotation is checked against your code, never against the data. Keeping “compile-time claim” and “runtime fact” separate in your head is one of the most useful mental models in this whole course.

Exercise: compile the example and inspect the output. List every TypeScript-only part that disappeared.

Lesson completed

Take this course offline

Get every free book, course edition, and software download.

Get the download library →