Type predicate that checks if this Result contains an error.
If true, TypeScript will narrow the type to include the error
property.
True if this Result contains an error, false if it contains a value
Type predicate that checks if this Result contains a success value.
If true, TypeScript will narrow the type to include the value
property.
True if this Result contains a value, false if it contains an error
Returns the contained value if Ok, otherwise returns the provided default value.
The value to return if this Result contains an error
The contained value if Ok, otherwise the default value
Transforms the contained value if Ok, otherwise returns the error unchanged. This is the functor map operation for Result.
The type of the transformed value
Function to transform the value if Ok
A new Result with the transformed value if Ok, otherwise the original error
Transforms the contained error if Error, otherwise returns the value unchanged. This allows for error transformation and chaining.
The type of the transformed error
Function to transform the error if Error
A new Result with the transformed error if Error, otherwise the original value
const failure = result.error(new Error("Original"));
const wrapped = failure.map_err(err => new Error(`Wrapped: ${err.message}`));
if (wrapped.is_error()) {
console.log(wrapped.error.message); // "Wrapped: Original"
}
const success = result.ok("value");
const unchanged = success.map_err(err => new Error("Won't be called"));
console.log(unchanged.is_ok()); // true
Pattern matches against the Result, executing the appropriate callback and returning its result. This is useful when you need to transform both Ok and Error cases into the same output type.
The return type of the on_ok callback
The return type of the on_error callback
Object containing callback functions for Ok and Error cases
Function to execute if Result is Ok, receiving the value
Function to execute if Result is Error, receiving the error
The result of the executed callback
const success = result.ok(42);
const message = success.match({
on_ok: (value) => `Value is ${value}`,
on_error: (error) => `Error occurred: ${error.message}`,
});
console.log(message); // "Value is 42"
const failure = result.error(new Error("Something went wrong"));
const errorMessage = failure.match({
on_ok: (value) => `Value is ${value}`,
on_error: (error) => `Error occurred: ${error.message}`,
});
console.log(errorMessage); // "Error occurred: Something went wrong"
Monadic bind operation. Chains another Result-returning operation if this Result is Ok. If this Result is Error, the function is not called and the error is propagated. This is also known as flatMap in some languages.
The type of the value in the returned Result
Function that takes the Ok value and returns a new Result
The Result returned by fn if Ok, otherwise the original error
function parseNumber(str: string): Result<number, Error> {
const num = Number(str);
return isNaN(num) ? result.error(new Error("Not a number")) : result.ok(num);
}
function divide(a: number, b: number): Result<number, Error> {
return b === 0 ? result.error(new Error("Division by zero")) : result.ok(a / b);
}
const computation = parseNumber("10")
.and_then(num => divide(num, 2));
if (computation.is_ok()) {
console.log(computation.value); // 5
}
Provides a fallback Result if this Result is Error. If this Result is Ok, the function is not called and the value is preserved. This allows for error recovery and alternative computation paths.
The type of error in the fallback Result
Function that takes the Error and returns a fallback Result
The fallback Result returned by fn if Error, otherwise the original Ok value
function tryPrimary(): Result<string, Error> {
return result.error(new Error("Primary failed"));
}
function tryFallback(): Result<string, Error> {
return result.ok("Fallback success");
}
const outcome = tryPrimary()
.or_else(() => tryFallback());
if (outcome.is_ok()) {
console.log(outcome.value); // "Fallback success"
}
Returns a generator that yields the contained value if this Result is Ok. If this Result is Error, the generator yields nothing (completes immediately). This allows for easy iteration over successful values in for-of loops and other iterator contexts.
A generator that yields the value if Ok, otherwise yields nothing
const success = result.ok(42);
for (const value of success) {
console.log(value); // 42
}
const failure = result.error(new Error("Failed"));
for (const value of failure) {
console.log("This won't execute");
}
// Useful for collecting successful values from multiple results
const results = [
result.ok(1),
result.error(new Error("Failed")),
result.ok(3)
];
const values = [];
for (const res of results) {
for (const value of res) {
values.push(value);
}
}
console.log(values); // [1, 3]
Returns the string tag for Object.prototype.toString calls. Always returns "Result" for Result instances.