Type Alias Result<A, E>

Result<A, E>: (Ok<A> | Err<E>) & IResult<A, E>

A value which can be of either type A or type E.

This is normally used as a return value for operations which can fail: E is short for Error. A can be thought of as the success value, and E is the error value. This is reflected in their constructors: to construct a success value, you call Ok, and to construct an error value, you call Err.

You can also use the Result.await function to convert a Promise that could throw an exception into a Promise that won't throw but instead returns a Result containing either the error or the expected result, or the Result.try function to run a function and catch any exceptions into a Result return value.

Type Parameters

  • A

    The type of the success value.

  • E

    The type of the error value.

function processResult(result: Result<string, Error>): void {
if (result.isOk()) {
console.info(result.value);
} else {
console.error(result.value.message);
}
}

processResult(Ok("Hello Joe!"));
processResult(Err(new Error("Get Robert to fix the bug!")));