@l3dev/result
v0.6.1
Published
Type-safe result-based error handling
Readme
result
Description
A Result based error handling library. Inspired by Rust's Result type and neverthrow.
This package aims to simplify neverthrow's API while still offering the result type-safety.
Installation
npm install @l3dev/resultpnpm add @l3dev/resultDocumentation
ok
Creates an Ok object, alias: Result.ok
ok<T>(value: T): Ok<T>Example
import { ok } from "@l3dev/result";
const result = ok(42);
result.ok; // true
result.value; // 42Use
NONEfor a void ok result:Ok<void>
err
Create an Err object, alias: Result.err
err<T>(type: T): Err<T, null>
err<T, D>(type: T, context: D): Err<T, D>Example
import { err } from "@l3dev/result";
const result = err("error");
result.ok; // false
result.type; // 'error'
result.context; // null
const resultWithContext = err("error", { message: "Something went wrong" });
result.ok; // false
result.type; // 'error'
result.context; // { message: 'Something went wrong' }Result.isOk
Returns true if the result is an Ok object
isOk(result: ReturnResult<any, any>): booleanResult.isErr
Returns true if the result is an Err object
isErr(result: ReturnResult<any, any>): booleanResult.fn
Wraps a function requiring that the ReturnType be a ReturnResult type.
fn<T, F extends (...) => T>(fn: F): FExample
import { ok, err, Result } from "@l3dev/result";
const myFunction = Result.fn((roll: number) => {
if (roll < 1 || roll > 6) {
return err("Invalid roll");
}
return ok(roll);
});
const result = myFunction(3); // Ok<3> | Err<"Invalid roll", null>Result.fromPromise
Catches any errors thrown by a promise into Err objects.
fromPromise<T>(promise: T): Promise<ReturnResult<Awaited<T>, ResultErrorDefinition<null, { error: TError }>>>Result.unwrapOrDefault
Unwraps the result, returning the value if it is an Ok object, or the default value if it is an Err object.
unwrapOrDefault<T>(result: ReturnResult<T, any>, defaultValue: T): T