TypeScript Academy · Lesson

The Result Pattern: Ok and Err

Return errors as values instead of throwing.

Lesson 2 of 413 steps

The Result Pattern: Ok and Err is a free TypeScript Academy lesson on CoddyKit. This is lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, and your progress syncs across the web and the CoddyKit app. The TypeScript Academy course includes 4 lessons in total.

Welcome

The Result pattern returns errors as values, enabling type-safe error handling without try/catch.

Result Type

Define a generic Result type with Ok and Err variants.
type Ok<T> = { ok: true; value: T };
type Err<E = Error> = { ok: false; error: E };
type Result<T, E = Error> = Ok<T> | Err<E>;

Helper Constructors

Create ok() and err() helpers for cleaner code.
const ok = <T>(value: T): Ok<T> => ({ ok: true, value });
const err = <E>(error: E): Err<E> => ({ ok: false, error });

Returning Results

Return Results from functions instead of throwing.
async function fetchUser(id: number): Promise<Result<User>> {
  try {
    const user = await db.findUser(id);
    if (!user) return err(new Error('Not found'));
    return ok(user);
  } catch (e) { return err(e as Error); }
}

Consuming Results

Check the ok flag before using the value.
const result = await fetchUser(1);
if (result.ok) {
  console.log(result.value.name); // User
} else {
  console.error(result.error.message);
}

mapResult Helper

Map over a successful Result to transform the value.
function mapResult<T, U, E>(r: Result<T, E>, fn: (v: T) => U): Result<U, E> {
  return r.ok ? ok(fn(r.value)) : r;
}

flatMapResult Helper

Chain Result-returning functions safely.
function flatMap<T, U, E>(r: Result<T, E>, fn: (v: T) => Result<U, E>): Result<U, E> {
  return r.ok ? fn(r.value) : r;
}

Result vs Exceptions

Exceptions are implicit and untyped. Results are explicit values in the type signature.

Library Support

Libraries like neverthrow, true-myth, and fp-ts provide production-ready Result types.

Result in API Handlers

Use Results in Express handlers for clean error propagation.
app.get('/users/:id', async (req, res) => {
  const result = await fetchUser(parseInt(req.params.id));
  if (result.ok) res.json(result.value);
  else res.status(404).json({ error: result.error.message });
});

Async Results

Use Promise> to combine async with the Result pattern.
type AsyncResult<T, E = Error> = Promise<Result<T, E>>;

Quick Check

What is the main advantage of the Result pattern over throwing exceptions?

Recap

The Result Pattern: Ok and Err: you learned the key concepts of this topic and how to apply them in real TypeScript projects.
Free to start

Learn TypeScript with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
101
Lessons
352

Frequently Asked Questions

Is the “The Result Pattern: Ok and Err” lesson free?

Yes — the full text of “The Result Pattern: Ok and Err” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO. The TypeScript Academy course includes 4 lessons in total.

What will I learn in “The Result Pattern: Ok and Err”?

Return errors as values instead of throwing. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start TypeScript Academy?

No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

How long does the “The Result Pattern: Ok and Err” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this TypeScript Academy lesson?

Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Typed Error Classes and Hierarchies
  2. The Result Pattern: Ok and Err
  3. Narrowing Caught Errors (unknown vs Error)
  4. Error Handling in Async TypeScript Code
← Back to TypeScript Academy