Skip to main content
Module 9 · Lesson 2 of 319/22 in the course~18 min
Module lessons (2/3)

Result<T, E>, Discriminated Errors & Exhaustive Checking

Lesson preview5 hands-on exercises readyCourse TypeScript · Module 9 — Error Handling, Result Pattern & Architecture

Make function signature failures explicit with Result<T, E> and ensure coverage with never.

What you'll learn in this lesson

  • Distinguish expected domain errors (validation, insufficient balance, missing record) from unexpected crashes (throw).
  • Express possible failures in function signature via Result<T, E> (Discriminated Union).
  • Building typed ok(value) / err(error) helpers and mapResult / unwrapOr combinators.
  • Verify with assertNever(value: never) that each domain error code is handled thoroughly.

Overview

When a function in TypeScript launches throw new Error(...), the signature function debit(amount: number): number reveals nothing to callers about whether it may fail due to insufficient balance or negative amount!

With the Result Pattern ({ ok: true; value: T } | { ok: false; error: E }), the error becomes an integral part of the return type: the caller cannot read value until it explicitly checks for if (res.ok)!

Sections covered in this lesson

  1. 1The Result<T, E> type and its constructors
  2. 2Consume Result<T, E> with Exhaustive Checking
  3. 3Guided practice
  4. 4Check for understanding
Pro Lesson · Module 9 — Error Handling, Result Pattern & Architecture

Unlock "Result<T, E>, Discriminated Errors & Exhaustive Checking" and the full Course TypeScript curriculum

Make function signature failures explicit with Result<T, E> and ensure coverage with never.

Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.

  • Instant access to all advanced modules in Course TypeScript plus 17 more courses
  • Hands-on browser exercises with automated test suites and solutions
  • 14-day money-back guarantee processed via Paddle.com

14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.