A synchronous Result class that represents either success (Ok) or failure (Err).
Provides method chaining for transformations and the take() pattern for
unwrapping values with early returns.
Example 1
Example 1
function divide(a: number, b: number): Result<number, ValidationError> { if (b === 0) { return Result.err(new ValidationError("Division by zero")); } return Result.ok(a / b); } const result = divide(10, 2) .map(x => x * 2) .map(x => x + 1); const value = result.take(); if (isErr(value)) return value; console.log(value); // 11
E extends BaseError
The type of the error (must extend BaseError)
Combines multiple Results into a single Result containing an array.
If all Results are Ok, returns Ok with an array of all values. If any Result is Err, returns the first Err encountered.
Returns the first Ok result from an array of Results.
If any Result is Ok, returns that Ok result. If all Results are Err, returns the last Err.
Creates a failed Result containing an error.
Type guard to check if a value is an Err Result.
This function has multiple overloads:
- Check if a Result is Err
- Check if any value (including from take()) is Err
Type guard to check if a value is an Ok Result.
Creates a successful Result containing a value.
try<T>(fn: () => T,context?: Record<string, unknown>): Result<T, UnexpectedError>
Wraps a function that might throw into a Result.
Catches any exceptions and wraps them in UnexpectedError.
_unsafeValue(): ResultValue<T, E>
Internal method to get the raw value (for testing/debugging). Not recommended for general use - prefer take() instead.
Chains operations that return Results (also known as flatMap).
If this Result is Ok, calls the function with the Ok value and returns its Result. If this Result is Err, returns the Err without calling the function.
Adds context to an Err result for early returns. Chainable with take() for adding context when propagating errors.
Performs a side effect on the Ok value without changing the Result.
inspectErr(fn: (error: E) => void): Result<T, E>
Performs a side effect on the Err value without changing the Result.
Transforms the Ok value using a mapper function, leaving Err untouched.
Transforms the Err value using a mapper function, leaving Ok untouched.
Pattern matching for Results - handle both Ok and Err cases.
Returns this result if Ok, otherwise returns the fallback result.
Returns this result if Ok, otherwise computes a fallback from the error.
Returns the Ok value or throws the Err error.
This is useful at process or demo boundaries where exception-style control
flow is acceptable and a caller wants to avoid repetitive take() /
isErr(...) branching.
Extracts the value from Ok or returns the Err for early returns.
This is the equivalent of Rust's ? operator. Use it with isErr() for
early returns in functions that return Results.
Returns either:
- The unwrapped value T if this result is Ok
- An Err Result<never, E> if this result is Err (can be directly returned)
unwrapOrElse<U>(fn: (error: E) => U): T | U
Returns the Ok value or computes a default from the error.