Result Types for Error Handling
Result Types for Error Handling — the core idea
A Result union represents expected success or failure as ordinary data instead of throwing for every domain outcome. A discriminant such as ok lets TypeScript narrow the corresponding fields and forces the caller to consider both cases.
What you’ll learn
- Explain result types for error handling 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: parsePort returns a tagged union. The first input converts to a positive integer and prints 8080. The second becomes NaN, so the error branch prints invalid port.
Keep the boundary clear: A Result is only as reliable as its validation logic. Converting input with Number alone does not ensure the value is within a useful port range.
See the idea as a flow
Run the complete TypeScript example
Save the code as result-types-error-handling.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 Result<T> = { ok: true; value: T } | { ok: false; error: string };
function parsePort(text: string): Result<number> {
const value = Number(text);
return Number.isInteger(value) && value > 0 ? { ok: true, value } : { ok: false, error: "invalid port" };
}
for (const input of ["8080", "oops"]) {
const result = parsePort(input);
console.log(result.ok ? result.value : result.error);
}Verified output
8080
invalid portBuild: npx tsc --strict --target es2022 --module commonjs --lib es2022,dom,esnext.disposable result-types-error-handling.ts; run: node result-types-error-handling.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
parsePort returns a tagged union. The first input converts to a positive integer and prints 8080. The second becomes NaN, so the error branch prints invalid port.
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
A Result is only as reliable as its validation logic. Converting input with Number alone does not ensure the value is within a useful port range.
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
Restrict the valid range to 1–65535 and return a typed result for "70000".
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
A Result union represents expected success or failure as ordinary data instead of throwing for every domain outcome.
Try the practice change and re-run the exact program to test your understanding.
Reaching the recap and completing the quizzes are separate milestones.