百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
O

optionals

> 编程语言
开源

Rust 类似的错误处理和 TypeScript 和 Deno 的选项!

193 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

Rust 类似的错误处理和 TypeScript 和 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 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

TypeScriptdenoerror-handlingrusttypescript

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

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