Built and signed on GitHub ActionsBuilt and signed on GitHub Actions
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.
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.
Methods:
Ok
: Creates anOk
Result instance.Err
: Creates anErr
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
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.
Add Package
deno add jsr:@oxi/result
Import symbol
import * as result from "@oxi/result";
Import directly with a jsr specifier
import * as result from "jsr:@oxi/result";
Add Package
pnpm i jsr:@oxi/result
pnpm dlx jsr add @oxi/result
Import symbol
import * as result from "@oxi/result";
Add Package
yarn add jsr:@oxi/result
yarn dlx jsr add @oxi/result
Import symbol
import * as result from "@oxi/result";
Add Package
npx jsr add @oxi/result
Import symbol
import * as result from "@oxi/result";
Add Package
bunx jsr add @oxi/result
Import symbol
import * as result from "@oxi/result";