Sub topic 1
Why TypeScript Types Are Not Enough at Runtime
TypeScript checks your code before it runs. It uses type annotations, interfaces, and type aliases to detect mistakes during development. These checks protect values that your code creates and controls.
They do not inspect data that arrives while the program is running. Data from an API, a form, a file, or an environment variable can have the wrong type or missing fields.
Prerequisites
You should know basic TypeScript types, interfaces, and type aliases. You should also be familiar with JavaScript objects, arrays, JSON, npm packages, ES module imports, and try/catch.
Compile-time types versus runtime data
Consider this type:
type User = {
id: number;
name: string;
};The type tells TypeScript what a valid User should look like. It does not add a check to JavaScript at runtime. The following assertion only tells the compiler to trust you:
const response = await fetch("/api/user");
const data = (await response.json()) as User;
console.log(data.name.toUpperCase());The server might return this instead:
{
"id": "not-a-number",
"name": null
}The assertion does not change the received value. It only suppresses some compiler warnings. Calling toUpperCase() on null then fails while the program is running.
JSON is especially important here. JSON.parse creates a JavaScript value from text, but it does not know which object shape your application expects.
Validation at the boundary
Runtime validation checks unknown data before the rest of your program uses it. A validation library such as Zod lets you describe the expected shape as executable code.
The boundary is the point where untrusted data enters your application:
flowchart TD
A[External data] --> B[Runtime validation]
B -->|valid| C[Trusted application data]
B -->|invalid| D[Validation error]
C --> E[Application logic]caption: Where runtime validation belongs alt: External data enters validation; valid data reaches application logic, while invalid data becomes a validation error.
A Zod schema is an object that describes and checks a value:
import { z } from "zod";
const UserSchema = z.object({
id: z.number(),
name: z.string(),
});
const unknownData: unknown = {
id: 42,
name: "Mina",
};
const user = UserSchema.parse(unknownData);
console.log(user.name.toUpperCase());The unknown type is useful at a boundary. It means the value exists, but your code must check what it is before using it. UserSchema.parse performs that check and returns data that matches the schema. If the value is invalid, parse throws a validation error instead of returning unsafe data.
This gives your application two separate protections:
- TypeScript checks how your code uses validated values.
- Zod checks whether runtime values actually match the expected shape.
A schema is therefore more than documentation. It is a runtime guard that runs against real input.
Choosing a boundary
Validate data immediately after receiving it and before passing it to business logic. Typical boundaries include:
- The result of
response.json() - Parsed request bodies in a server
- Values read from
process.env - Form submissions
- Configuration files and message queues
Keep internal values typed normally after validation. Rechecking every local variable usually adds noise. The important rule is to avoid treating unknown external data as trusted merely because its expected shape is written in TypeScript.
Exercise
Why can (await response.json()) as User still produce a runtime error?
Show answer
A type assertion affects only TypeScript's compile-time checking. It does not inspect the response, so the value may still have missing fields or incorrect types.