Ok is a type that represents a successful result.
Creates an Ok.
The value to be wrapped.
const result = new Ok(42); Copy
const result = new Ok(42);
Returns a Result. If this Result is an Ok, the function returns passed Result.
The Result to be returned.
A Result that is the result of applying the given function to the value of this Result.
const result1 = new Ok(42);const result2 = new Ok('hello');const combined = result1.and(result2); // combined is result2 Copy
const result1 = new Ok(42);const result2 = new Ok('hello');const combined = result1.and(result2); // combined is result2
Returns a new Result that is the result of applying the given function to the value of this Result.
The function to be applied to the value of this Result.
const result = new Ok(42);const newResult = result.andThen(value => new Ok(value + 1)); // newResult is Ok(43) Copy
const result = new Ok(42);const newResult = result.andThen(value => new Ok(value + 1)); // newResult is Ok(43)
Returns true if this Result is an Err, false otherwise.
false as this Result is an Ok.
const result = new Ok(42);console.log(result.isError()); // false Copy
const result = new Ok(42);console.log(result.isError()); // false
Returns true if this Result is an Ok, false otherwise.
true if this Result is an Ok, false otherwise.
const result = new Ok(42);console.log(result.isOk()); // true Copy
const result = new Ok(42);console.log(result.isOk()); // true
const result = new Ok(42);const mappedResult = result.map(value => value.toString()); // mappedResult is Ok('42') Copy
const result = new Ok(42);const mappedResult = result.map(value => value.toString()); // mappedResult is Ok('42')
Returns a new Result that is the result of applying the given function to the error of this Result.
The function to be applied to the error of this Result.
This Result as it is an Ok.
const result = new Ok(42);const mappedErrorResult = result.mapError(error => 'new error'); // mappedErrorResult is still Ok(42) Copy
const result = new Ok(42);const mappedErrorResult = result.mapError(error => 'new error'); // mappedErrorResult is still Ok(42)
Returns the value of this Result.
The value of this Result.
const result = new Ok(42);console.log(result.unwrap()); // 42 Copy
const result = new Ok(42);console.log(result.unwrap()); // 42
Throws an error as this Result is an Ok.
If this Result is an Ok.
const result = new Ok(42);result.unwrapError(); // throws Error Copy
const result = new Ok(42);result.unwrapError(); // throws Error
The value to be returned if this Result is an Err.
const result = new Ok(42);console.log(result.unwrapOr(0)); // 42 Copy
const result = new Ok(42);console.log(result.unwrapOr(0)); // 42
Ok is a type that represents a successful result.