Hello! Last lesson, you distinguished TypeScript source code from the JavaScript that Node.js executes. This lesson examines what is lost in that handoff—and why it matters when a backend receives data it did not create.
By the end, you should be able to explain type erasure and answer a practical interview question: “If our request body has a TypeScript type, why must the API still validate it?”
Checking code is different from running code
TypeScript performs static type checking: it examines your source code before the program runs. If you declare that a membership ID is a number and then assign it a string literal, TypeScript can report the mismatch while you develop:
type Membership = {
id: number
active: boolean
}
const membership: Membership = {
id: "7", // TypeScript reports a type error here.
active: true,
}
The checker can see both the declared type and the literal "7". It does not need to start a server to spot the problem.
A running server faces a different situation. Its code may have been checked earlier, but a client sends a request later. TypeScript cannot inspect every future request while checking your source files. The distinction is between what the checker can establish about written code and what the server must discover about values arriving at runtime.
What is TypeScript? What is its relation to JavaScript? Runtime vs Compile Time | TS Build Cycle
Watch “What is TypeScript? What is its relation to JavaScript? Runtime vs Compile Time | TS Build Cycle” from Coding Garden for a visual account of those two phases.
Start with static checking, where the presenter distinguishes examining source code from executing it. Then watch the build example: focus on the typed function in the source and the absence of its type annotations in the resulting JavaScript.
The important point is not that TypeScript types are unreliable. They are useful for checking the code you write. The point is that a source-code check is not a check performed on each value that later reaches your server.
What “type erasure” actually removes
Consider a valid TypeScript function:
type Membership = {
id: number
active: boolean
}
function isActive(membership: Membership): boolean {
return membership.active
}
After compilation, the relevant JavaScript is approximately:
function isActive(membership) {
return membership.active
}
The Membership type, the parameter annotation, and the return annotation helped the checker reason about the source. They are not JavaScript instructions, so they do not appear in the code Node.js executes. The property access membership.active does remain: it is an instruction that does something at runtime.
This removal is called type erasure. It does not mean that runtime values lack JavaScript types: a value can still be a string, number, boolean, or object, and JavaScript can inspect it with operations such as typeof. It means that TypeScript’s declarations do not remain as automatic runtime rules. The emitted function does not secretly ask whether its argument satisfies Membership.
The TypeScript documentation states the design principle and its consequence directly.
Documentation - TypeScript for the New Programmer
Read the TypeScript documentation’s explanation of runtime behavior and erased types to connect the source-and-output example to the language’s design.
Find the subsections “Runtime Behavior” and “Erased Types.” Read both explanations, beginning with the paragraph under “Runtime Behavior” and ending with the paragraph explaining that types do not affect execution. Focus on why checking a type does not insert a corresponding JavaScript check.
The following image is a useful visual summary, with one caution: its middle panel is schematic, not a literal example of emitted code. In particular, defining an interface does not cause the compiler to emit const User = undefined; it simply emits no JavaScript declaration for that interface.

Why design it this way? TypeScript builds on JavaScript while keeping JavaScript’s runtime behavior. It helps you find mistakes before execution without automatically adding a validator for every declared type. If you want a check to happen while the server runs, you must include executable checking code.
An API request crosses the boundary of what was checked
Imagine a backend that expects this JSON request body:
{"id":7,"active":false}
It might instead receive:
{"id":"7","active":"false"}
Both are valid JSON. But the second has a string ID and a string "false", not the number and boolean described by Membership. A TypeScript declaration in the backend cannot change what the client sent.
Here is a particularly dangerous way to handle that data:
const raw: unknown = JSON.parse('{"id":"7","active":"false"}')
const membership = raw as Membership
if (membership.active) {
console.log("Active membership")
}
The as Membership assertion tells the TypeScript checker to treat raw as that type. It does not convert "7" into a number or "false" into a boolean, and it does not reject the payload. The assertion disappears from the JavaScript just as the type annotations did.
This code prints Active membership. In JavaScript, the nonempty string "false" is truthy, even though the boolean false is not. A type assertion has made the source look reassuring without changing the value that the backend actually received.
The video below demonstrates a related failure: code accepts data from outside the checked source, then encounters a missing property at runtime. Its final view of the JavaScript output makes the cause visible.
Fixing TypeScript's Blindspot: Runtime Typechecking
Watch “Fixing TypeScript's Blindspot: Runtime Typechecking” by Jack Herrington for a concrete failure caused by trusting external data, followed by a view of the compiled JavaScript.
Watch the input contrast: first note the error for an invalid value written directly in the TypeScript code, then the failure after JSON is read from a file. Continue through the JavaScript output, focusing on what happened to the type information that might have appeared to protect the function.
A file and an HTTP request illustrate the same boundary: their contents are determined outside the TypeScript source that was checked. The same caution applies to data from another API, configuration, or user input. A type annotation describes what your code expects; it is not evidence that an external producer met that expectation.
Validation must be executable
Runtime validation means inspecting the value that actually arrived and deciding whether to accept or reject it. For the membership example, a backend would need to establish at least that the payload is an object with an id of the expected numeric form and an active value that is genuinely a boolean. Application rules might require further checks—for example, whether an ID is a valid positive integer.
A check such as this runs when the program runs:
typeof value === "boolean"
A declaration such as this does not:
const value: boolean = incomingValue
The first is a JavaScript expression that can inspect a particular value. The second is a TypeScript claim that can help check your source, but does not validate incomingValue after compilation. In practice, a backend validates the whole expected shape, not just one field, and rejects an invalid request before using its contents for decisions or database updates. We will write fuller checks in later lessons; for now, the essential distinction is when the check occurs.
This gives you a useful way to think about trust. Within checked code, TypeScript helps catch inconsistent uses of values. At an input boundary, the incoming value is not yet known to match your application’s types. Once executable validation has confirmed its shape, the rest of the application can use that result with greater confidence. Neither an annotation nor as Membership performs that boundary check.
For an interview, a concise explanation is:
TypeScript checks source code before it runs. Its type declarations are erased when the code becomes JavaScript, so Node.js cannot use them to verify a request body. Because clients can send values that differ from our declared types, the backend must validate incoming data at runtime before relying on it.
Conclusion
Type erasure removes TypeScript’s type annotations and declarations from the JavaScript that executes. Static checking can catch mistakes visible in your code, but it cannot certify future API payloads. A type assertion changes what the checker believes; only executable validation checks the value that arrived.
Next, we will use another runtime distinction to decide whether a piece of code belongs in the browser or in Node.js: which APIs its execution environment provides.