Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
O

optionals

> 编程语言
Open source

Rust-like error handling and options for TypeScript and Deno!

193 stars0 likes1 views
WebsiteGitHub

About

Rust-like error handling and options for TypeScript and Deno!


Remove null and undefined from your projects with the safety of Rust's Option and Result enums. Full pattern matching, exhaustive handling, and instanceof guards, with zero runtime dependencies. Runs on Bun, Deno, and Node.

Why?

Returning null/undefined blurs "returned nothing" with "returned an accidental null". Throwing exceptions makes error handling implicit. Rust's Option/Result fix both by forcing you to handle every case at compile time. This library brings that ergonomics to TypeScript.

typescript
function divide(left: number, right: number): Option {
  if (right === 0) return None();
  return Some(left / right);
}

function parse(input: string): Result {
  const n = Number(input);
  return Number.isNaN(n) ? Err("not a number") : Ok(n);
}

Install

Bun

bash
bun add rust-optionals
typescript
import { Option, Some, None, Result, Ok, Err } from "rust-optionals";

Deno

typescript
import {
  Option,
  Some,
  None,
  Result,
  Ok,
  Err,
} from "jsr:@oliverbrotchie/[email protected]";

Node

bash
npm install rust-optionals
typescript
import { Option, Some, None, Result, Ok, Err } from "rust-optionals";

Option

Wrap a value that may not exist.

Method Description
Some(value) Construct an Option holding a value.
None() Construct an empty Option.
.isSome() / .isNone() Test which variant is held.
.unwrap() / .expect(msg) Extract the value (throws on None).
.unwrapOr(fb) / .unwrapOrElse(fn) Extract or fall back.
.map(fn) Transform a held value.
.inspect(fn) Run a side effect on a held value, returning the Option.
.or(other) Coalesce alternatives.
.okOr(err) Convert to a Result.
.peek() Read the value for switch matching.
Option.from(value) / .fromAsync(fn) Wrap a value (or closure), mapping null/undefined to None.
typescript
const x: Option = Some(42); // call syntax
const y = new Some(42); // or `new`; both produce a `Some`
x.map((n) => n + 1).unwrap(); // 43
None().unwrapOr(0); // 0

// instanceof guards work on the constructors:
if (x instanceof Some) {
  /* ... */
}

Result

Represent either a success (Ok) or a failure (Err).

Method Description
Ok(value) / Err(error) Construct a Result.
.isOk() / .isErr() Test which variant is held.
.unwrap() / .expect(msg) Extract the Ok value (throws on Err).
.unwrapErr() / .expectErr(msg) Extract the error.
.unwrapOr(fb) / .unwrapOrElse(fn) Extract or fall back.
.map(fn) / .mapErr(fn) Transform the Ok or Err side.
.throw() Throw the held error.
.ok() Convert to an Option.
Result.from(fn) / .fromAsync(fn) Capture a closure's result or thrown error.
Result.partition(results) Split an array into { ok, err }.
typescript
const ok: Result = Ok(42); // call syntax
const alt = new Ok(42); // or `new`
ok.map((n) => n * 2).unwrap(); // 84

const err: Result = Err("boom"); // string coerced to Error
err.isErr(); // true

Result.partition([Ok(2), Err("e"), Ok(16)]);
// { ok: [2, 16], err: [Error("e")] }

Errors are detected for plain Error, cross-realm errors, and custom subclasses (instanceof Error or Error.isPrototypeOf).

Matching with peek()

Use peek() to dispatch on the held value without consuming the Option or Result:

typescript
function coolOrNice(input: Option): Option {
  switch (input.peek()) {
    case "cool":
      return Some();
    case "nice":
      return Some();
    default:
      return None();
  }
}

Development

bash
deno test   # Deno
bun test    # Bun
deno task build  # emit the npm package (Bun/Node)

License

MIT

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

TypeScriptdenoerror-handlingrusttypescript

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言