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
- The Problem with Throwing Errors
- Modeling Result Types
- Option and Maybe Types
- Railway-Oriented Programming