The Result pattern and typed errors
Go from the first annotated variable to writing your own conditional, mapped and infer-based utility types, while keeping a clear line between compile-time checking and runtime behavior.
What you will be able to do
#- Model success and failure as a discriminated union.
- Chain results without throwing.
- Decide between a Result and exceptions.
Overview
#A Result type
type Result<T, E = Error> =
| { ok: true; value: T }
| { ok: false; error: E };
The discriminant ok narrows each branch.
Constructors and combinators
const ok = <T>(value: T): Result<T, never> => ({ ok: true, value });
const err = <E>(error: E): Result<never, E> => ({ ok: false, error });
function map<T, U, E>(r: Result<T, E>, f: (value: T) => U): Result<U, E> {
return r.ok ? ok(f(r.value)) : r;
}
Why use it
A Result makes failure part of the type, so the caller must handle it. It is common in validation, parsing and API layers where errors are expected, not exceptional.
When to throw
Throw for programming errors and unrecoverable states: a missing dependency, an invariant violation. Return a Result for expected failures the caller can act on.
Cost
Every call site must branch. Wrapping everything in a Result adds noise; use it where the caller genuinely can recover.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Implement
Implement `unwrapOr<T, E>(result: Result<T, E>, fallback: T): T`.
starter.typescriptexport type Result<T, E = Error> = | { ok: true; value: T } | { ok: false; error: E }; export function unwrapOr<T, E>(result: Result<T, E>, fallback: T): T { // your code }Reveal the reference answer
reference.typescriptexport type Result<T, E = Error> = | { ok: true; value: T } | { ok: false; error: E }; export function unwrapOr<T, E>(result: Result<T, E>, fallback: T): T { return result.ok ? result.value : fallback; }Authored expected output: Compiles with no errors. Runtime: unwrapOr({ ok: true, value: 2 }, 0) is 2; unwrapOr({ ok: false, error: "x" }, 0) is 0.
The ok discriminant narrows the union so value is available only in the success branch.
Exercise 2Predict
What is the type of `value` inside the `if`? ```ts type Result<T> = { ok: true; value: T } | { ok: false; error: string }; declare const r: Result<number>; if (r.ok) { const value = r.value; } ```
Exercise 3Interview
Explain when you would return a Result instead of throwing, using a validation pipeline as the example.
Reveal the reference answer
reference.typescriptIn a validation pipeline, invalid input is expected, not exceptional, and the caller usually wants to show every error rather than stop at the first throw. A Result carries the failure as data, so the pipeline can accumulate errors and the caller must handle the failure branch. Reserve throws for programming errors such as a missing dependency, where continuing is meaningless.Authored expected output: A design answer separating expected failures from programming errors.
The choice follows from whether the caller can recover.
Common misconceptions
#- A Result removes the need for error handling.
- A Result is faster than a throw.
- ok and error can both be present.
Follow-up practice
#- typescript-async-typing
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.