Understanding Type-Level Programming
Understanding Type-Level Programming — the core idea
Type-level programming describes relationships between types instead of computing runtime values. A generic type alias can transform a collection of possible shapes while the resulting JavaScript contains only ordinary values. Use type logic to make incorrect combinations harder to express, not as a replacement for parsing user input.
What you’ll learn
- Explain understanding type-level programming with a concrete use case.
- Run the example and account for every line of its output.
- Recognize the difference between compile-time checking and runtime behavior.
Understand the rules and when to use them
Reading the code: The generic ResponseOf<T> constructs a compile-time union. Choosing User substitutes a concrete data shape. The success discriminator narrows reply, then JavaScript reads id and name and prints the result.
Keep the boundary clear: Writing a type alias does not create a constructor or perform runtime checking; untrusted JSON can still have the wrong shape.
See the idea as a flow
Run the complete TypeScript example
Save the code as type-level-programming.ts. The result below was captured after compiling this exact example with strict TypeScript checking and executing the emitted JavaScript in Node.js.
TypeScript code
type ResponseOf<T> = { data: T; success: true } | { error: string; success: false };
type User = { id: number; name: string };
const reply: ResponseOf<User> = { success: true, data: { id: 7, name: "Ada" } };
if (reply.success) console.log(`${reply.data.id}: ${reply.data.name}`);Verified output
7: AdaBuild: npx tsc --strict --target es2022 --module commonjs --lib es2022,dom,esnext.disposable type-level-programming.ts; run: node type-level-programming.js. The exact filename may be changed freely. When a multi-file project exists, prefer its tsconfig.json and build scripts.
Trace the example line by line
The generic ResponseOf<T> constructs a compile-time union. Choosing User substitutes a concrete data shape. The success discriminator narrows reply, then JavaScript reads id and name and prints the result.
The output shown above comes from the initialized values in this program. Replacing a type annotation alone does not transform a JavaScript value.
Avoid this common mistake
Writing a type alias does not create a constructor or perform runtime checking; untrusted JSON can still have the wrong shape.
Check both the TypeScript diagnostic and the emitted JavaScript behavior. A program can compile correctly and still need runtime validation or error handling.
Practice independently
Make ResponseOf<number> and write success and error values. Check that each branch only accesses fields valid in that branch.
Before running your change, write down the type you expect TypeScript to infer and the output you expect JavaScript to print. Then check both predictions.
Check your understanding
What to remember
Type-level programming describes relationships between types instead of computing runtime values.
Try the practice change and re-run the exact program to test your understanding.
Reaching the recap and completing the quizzes are separate milestones.