Skip to main content


A TypeScript implementation of the Rust-inspired Result type, offering a robust and functional approach to error handling and success management in asynchronous and synchronous operations.

Works with
This package works with Bun
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Browsers
JSR Score
a month ago
deno add @oxi/result
import * as mod from "@oxi/result";
npx jsr add @oxi/result
import * as mod from "@oxi/result";
yarn dlx jsr add @oxi/result
import * as mod from "@oxi/result";
pnpm dlx jsr add @oxi/result
import * as mod from "@oxi/result";
bunx jsr add @oxi/result
import * as mod from "@oxi/result";

This module provides the Result class, a TypeScript implementation of the Rust's Result type, offering a robust way to handle errors and success values without throwing exceptions. It is particularly useful in functional programming patterns and scenarios where errors are expected and need to be handled gracefully.

The Result type is a union type that can either be Ok, representing success and containing a success value, or Err, representing failure and containing an error value.


  • Ok: Creates an Ok Result instance.
  • Err: Creates an Err Result instance.
  • from: Converts a function's return value or a promise to a Result.
  • wrap: Wraps a function, ensuring it returns a Result.
  • match: Applies functions based on the Result's state (Ok or Err).
  • isOk, isErr: Check the state of the Result.
  • map, mapErr: Transform the value inside the Result.
  • and, or, andThen, orElse: Combine Result instances in various ways.
  • unwrap, unwrapOr, unwrapOrElse: Extract values from the Result, with or without defaults.
  • expect, expectErr: Unwrap with custom error messages.
  • toJSON: Serialize the Result to a JSON object.
  • toString: Get a string representation of the Result.


Example 1

// Creating an Ok Result
const okResult = Result.Ok(123);
console.log(okResult.toString()); // "Ok(123)"

// Creating an Err Result
const errResult = Result.Err("An error occurred");
console.log(errResult.toString()); // "Err(An error occurred)"

// Handling a Result with `match`
const result = Result.Ok(5);
const message = result.match(
  value => `Success with value: ${value}`,
  error => `Failed due to error: ${error}`
console.log(message); // "Success with value: 5"

// Using `map` to transform an Ok value
const doubled = Result.Ok(2).map(x => x * 2);
console.log(doubled.unwrap()); // 4

// Using `unwrapOr` to provide a default value for an Err
const value = Result.Err("Error").unwrapOr(42);
console.log(value); // 42

// Using `from` to handle asynchronous operations
async function fetchData() {
  return "Data";
Result.from(fetchData).then(res => console.log(res.unwrap())); // "Data"

// Combining Results with `andThen`
const queryResult = Result.Ok("Query");
const processResult = queryResult.andThen(query => Result.Ok(`Processed ${query}`));
console.log(processResult.unwrap()); // "Processed Query"

This module is designed to enhance error handling and functional programming patterns in TypeScript applications, providing a more declarative approach to dealing with operations that can succeed or fail.

Built and signed on
GitHub Actions
View transparency log