Skip to main content

@neoangelism/tsrs@0.0.2

latest

tsrs - a typescript library that emulates rust's Result and Option types

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
94%
Published
3 weeks ago
indexResult
class Result
deno add @neoangelism/tsrs
import { Result } from "@neoangelism/tsrs";
npx jsr add @neoangelism/tsrs
import { Result } from "@neoangelism/tsrs";
yarn dlx jsr add @neoangelism/tsrs
import { Result } from "@neoangelism/tsrs";
pnpm dlx jsr add @neoangelism/tsrs
import { Result } from "@neoangelism/tsrs";
bunx jsr add @neoangelism/tsrs
import { Result } from "@neoangelism/tsrs";

Result is a type that represents either success (Ok) or failure (Err).

Constructors

new
Result(t: Ok<T> | Err<E>)

Constructs a new Result instance.

Type Parameters

Methods

expect(message: string): T

Unwraps the Ok value or throws an error with a custom message.

Checks if the result is Err.

Checks if the result is Ok.

match<U>(okCallback: (value: T) => U, errCallback: (err: E) => U): U
ok(): Option<T>

Converts the Result to an Option.

Unwraps the Ok value.

unwrapOr(fallback: T): T

Unwraps the Ok value or returns a fallback value.

unwrapOrElse(cb: () => T): T

Unwraps the Ok value or computes a fallback value by invoking a function.

Static Methods

fromPromise<T>(p: Promise<T>): Promise<Result<T, unknown>>

Creates a new Result from a Promise.

fromThrow<U, V>(fn: () => U, err: V): Result<U, V>

Creates a new Result from a function that can throw.