The type of the given value.
The type of the computed value.
A closure that matches the given value for a specific condition.
The type of the given value.
A closure that returns a value.
The type of the given value.
Invokes a function that may throw an error and returns a Result
.
The given function is invoked within a try/catch block. If an error is caught, it is returned
as an instance of Err
. If no error is thrown, then the result is returned as an instance of
Ok
.
It is important to know that this function is unable to determine the proper type of the thrown error. While the type defaults to the built-in JavaScript
Error
type, it is very much encouraged that you specify the error type.
// A simple function that can throw an error.
const throws = (fail: boolean) => {
if (fail) {
throw new Error("An example error.");
}
return "example";
};
// An example of the error being caught.
let result: Result<string, Error> = attempt(() => throws(true));
assert(result.isErr() === true);
// An example of the result being returned.
result = attempt(() => throws(false));
assert(result.isOk() === true);
The type of the function's return value.
The type of the thrown value.
The function to invoke.
A Result
for the function return value.
Creates a new instance of Err
.
let result: Result<string, string> = err('error');
assert(result.isErr() === true);
The type of the success value.
The type of the failure value.
The failure value.
The instance.
Wraps a value as an Option
.
This function provides a convenient way of wrapping the result of a function that could return
null
or undefined
. If null
or undefined
is given, an instance of None
is returned. If
any other value is provided, including other falsy values, an instance of Some
is returned.
let option: Option<string> = maybe(null);
assert(option.isNone() === true);
option = maybe("example");
assert(option.isSome() === true);
The type of the value.
The instance.
Creates a new instance of None
.
let option: Option<string> = none();
assert(option.isNone() === true);
The type of the value.
The instance.
Creates a new instance of Ok
.
let result: Result<string, string> = ok('value');
assert(result.isOk() === true);
The type of the success value.
The type of the failure value.
The success value.
The instance.
Creates a new instance of Some
.
let option: Option<string> = some('example');
assert(option.isSome() === true);
The type of the value.
The value to wrap.
The instance.
Generated using TypeDoc
A closure that computes a new value based on the given value.