Module lessons (2/3)
Result<T, E>, Discriminated Errors & Exhaustive Checking
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 andmapResult/unwrapOrcombinators. - 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
- 1The
Result<T, E>type and its constructors - 2Consume
Result<T, E>with Exhaustive Checking - 3Guided practice
- 4Check for understanding
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.