Result types

Ok and Err shapes

A result is a union with two honest outcomes: ok with a value, or err with a message. Callers see both cases in the type, so no failure hides behind undefined.

The ok flag drives narrowing. Check it once and the checker knows exactly which fields exist on each branch. The program builds one result of each shape and prints them.

type Result<T> = | { ok: true; value: T } | { ok: false; error: string }; function ok<T>(value: T): Result<T> { return { ok: true, value }; } function err<T>(error: string): Result<T> { return { ok: false, error }; } console.log(ok(1), err("bad"));

{ ok: true, value: 1 } { ok: false, error: 'bad' }

Handle both

Every result demands a decision on both branches. An if on the ok flag splits the world cleanly: happy path with the value, sad path with the message.

Handle failures near the screen where a fallback exists. Early returns keep the happy path flat instead of nested three levels deep. The program shows a good result and a failed one.

type Result<T> = | { ok: true; value: T } | { ok: false; error: string }; function ok<T>(value: T): Result<T> { return { ok: true, value }; } function err<T>(error: string): Result<T> { return { ok: false, error }; } function show(r: Result<string>): string { if (r.ok) { return r.value; } return "Problem: " + r.error; } console.log(show(ok("fine")), show(err("bad")));

fine Problem: bad

Chain results

Chaining runs the next step only when the previous one succeeded. A small helper maps the inner value and passes errors through untouched.

This pattern removes pyramids of nested checks. Each step states its transform while the helper carries the plumbing between steps. The program doubles the value inside a result and prints the new result.

type Result<T> = | { ok: true; value: T } | { ok: false; error: string }; function ok<T>(value: T): Result<T> { return { ok: true, value }; } function mapResult<A, B>(r: Result<A>, fn: (v: A) => B): Result<B> { if (!r.ok) { return r; } return ok(fn(r.value)); } const doubled = mapResult(ok(21), (v) => v * 2); console.log(doubled);

{ ok: true, value: 42 }

Throw at the edge

Results work best inside logic, while outer layers often expect exceptions. Convert once at the edge: unwrap values on ok and throw a shaped error on err.

One conversion point keeps inner code total and outer code familiar. Debugging stays simple because throws happen in exactly one known place. The program unwraps a good result, then unwraps a failed one and catches the message.

type Result<T> = | { ok: true; value: T } | { ok: false; error: string }; function ok<T>(value: T): Result<T> { return { ok: true, value }; } function err<T>(error: string): Result<T> { return { ok: false, error }; } function unwrap<T>(r: Result<T>): T { if (!r.ok) { throw new Error(r.error); } return r.value; } console.log(unwrap(ok(7))); try { unwrap(err("boom")); } catch (e: unknown) { console.log((e as Error).message); }

7
boom

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does a result type make visible to the caller?

    Answer

    Both outcomes: a value on ok and a message on err

    The union names both branches, so a failure cannot hide behind undefined.

  2. Why does the ok flag narrow the value?

    Answer

    It is a literal type, so the compiler knows the branch

    A true or false literal in each member of the union tells the compiler which fields exist.

  3. What does mapResult do with a failed result?

    Answer

    It passes the failure through untouched

    The transform runs only on a value, so a failure keeps its message for the edge.

  4. Why convert a result into a throw only at the edge?

    Answer

    Inner code stays total, and throws happen in one known place

    One conversion point keeps the logic easy to reason about and the outer API familiar.

  5. What does this program print? type Result<T> = { ok: true; value: T } | { ok: false; error: string }; function read(r: Result<number>): number | string { return r.ok ? r.value : r.error; } console.log(read({ ok: true, value: 3 }));

    Answer

    3

    The flag is true, so the true branch reads the value and prints 3.

  6. What does this program print? type Result<T> = { ok: true; value: T } | { ok: false; error: string }; function show(r: Result<string>): string { if (r.ok) { return r.value; } return "Problem: " + r.error; } console.log(show({ ok: false, error: "bad" }));

    Answer

    Problem: bad

    The failed result takes the other branch, and the message is prefixed.

  7. What does this program print? type Result<T> = { ok: true; value: T } | { ok: false; error: string }; function mapResult<A, B>(r: Result<A>, fn: (v: A) => B): Result<B> { if (!r.ok) { return r; } return { ok: true, value: fn(r.value) }; } console.log(mapResult({ ok: true, value: 4 }, (v) => v + 1));

    Answer

    { ok: true, value: 5 }

    The transform adds one, and the helper wraps the result in a new ok object.

  8. What does this program print? type Result<T> = { ok: true; value: T } | { ok: false; error: string }; function unwrap<T>(r: Result<T>): T { if (!r.ok) { throw new Error(r.error); } return r.value; } try { console.log(unwrap({ ok: false, error: "boom" })); } catch (e: unknown) { console.log((e as Error).message); }

    Answer

    boom

    The failed result throws, and the catch prints the message of that error.

  9. What does the compiler say about this program? type Result<T> = { ok: true; value: T } | { ok: false; error: string }; const good: Result<number> = { ok: true, value: 3 }; console.log(good.error);

    Answer

    error TS2339: Property 'error' does not exist on type '{ ok: true; value: number; }'.

    Only the failed member of the union has an error field, and this value is the other one.

  10. What does the compiler say about this program? type Result<T> = { ok: true; value: T } | { ok: false; error: string }; function unwrap<T>(r: Result<T>): T { if (!r.ok) { throw new Error(r.error); } return r.error; } console.log(unwrap);

    Answer

    error TS2339: Property 'error' does not exist on type '{ ok: true; value: T; }'.

    After the early throw the value is the ok member, which has a value and no error.

Article author: Arthur Isaev