This module provides the Result type, a versatile utility to handle success
(Ok
) and error (Err
) outcomes in TypeScript. The Result type enables safe
and expressive error handling in your code, especially useful for operations
that might fail, without throwing exceptions.
The package is inspired by Rust's
Result Enum
, and it provides
similar methods.
Install the Result module using npm:
npx jsr add @skookumchoocher/result
And import it in your code:
import { Result } from "@skookumchoocher/result";
Install the Result
module using deno:
deno add jsr:@skookumchoocher/result
And import it in your code:
import { Result } from "@skookumchoocher/result";
The Result<T, E>
type can represent:
Ok
: Success, containing a value of type T.Err
: Failure, containing an error of type E.
import { Result } from "./mod.ts";
import { assertEquals } from "@std/assert";
const success = Result.ok(42);
assertEquals(success.ok, 42);
const failure = Result.err("error");
assertEquals(failure.err, "error");
These methods provide a type guard for the variant.
isOk()
: Returns true if the result isOk
.isErr()
: Returns true if the result isErr
.
import { Result } from "./mod.ts";
import { assert } from "@std/assert";
const success = Result.ok(42);
assert(success.isOk());
const failure = Result.err("error");
assert(failure.isErr());
map
: Apply a function to the value inOk
, leavingErr
unchanged.mapErr
: Apply a function to the value inErr
, leavingOk
unchanged.
import { Result } from "./mod.ts";
import { assertEquals } from "@std/assert";
const success = Result.ok(2);
const mapped = success.map((x) => x * 2);
assertEquals(mapped, Result.ok(4));
const failure = Result.err("error");
const mappedErr = failure.mapErr((e) => `${e}!`);
assertEquals(mappedErr, Result.err("error!"));
unwrap
: Retrieve theOk
value or throw an error ifErr
.unwrapOr
: Retrieve theOk
value or return a default value.expect
: Retrieve theOk
value or throw a custom error ifErr
.
import { Result } from "./mod.ts";
import { assertEquals } from "@std/assert";
const value = Result.ok(10).unwrap();
assertEquals(value, 10);
const defaultValue = Result.err("error").unwrapOr(0);
assertEquals(defaultValue, 0);
const result = Result.ok(10) as Result<number, string>);
const expectation = result.expect("Expected value");
assertEquals(expectation, 10);
Result also supports asynchronous mapping and inspection:
mapAsync
: Apply an async function to theOk
value.inspectAsync
: Run an async function on theOk
orErr
value.
import { Result } from "./mod.ts";
import { assertEquals } from "@std/assert";
const asyncResult = await Result.ok(2).mapAsync(async (x) => x * 2);
assertEquals(asyncResult, Result.ok(4));
const asyncResult2 = await Result
.err("error")
.inspectErrAsync(async (e) => console.error(e));
assertEquals(asyncResult2, Result.err("error"));
This function takes an iterable
of Results. If all elements are Ok
, it
returns an Ok
containing an array of values; if any Err
is encountered, it
returns the first Err
.
import { Result } from "./mod.ts";
import { assertEquals } from "@std/assert";
const results = [Result.ok(1), Result.ok(2)];
assertEquals(Result.fromIter(results), Result.ok([1, 2]));
const mixed = [Result.ok(1), Result.err("error")];
assertEquals(Result.fromIter(mixed), Result.err("error"));
For a full list of methods and examples, refer to the documentation in the code
comments from the internal
Resultable interface. The Ok
and
Err
classes inheret the doc comments from the Resultable
interface.
MIT, similar to Rust's Result type.