0Pricing
TypeScript Academy · Lesson

Modeling Result Types

Represent success and failure as data with Result.

The Result Type

A Result models an operation that either succeeds with a value or fails with an error, as a discriminated union you can return instead of throwing.

Defining Result

The classic shape uses an ok discriminant. When ok is true there is a value; when false there is an error.

type Result<T, E> =
  | { ok: true; value: T }
  | { ok: false; error: E };

All lessons in this course

  1. The Problem with Throwing Errors
  2. Modeling Result Types
  3. Option and Maybe Types
  4. Railway-Oriented Programming
← Back to TypeScript Academy