Skip to content
0xC0DE666Public

About

Rust-like Result and Option Utilities for TypeScript

Resources

Stars

3 stars

Watchers

0 watching

Forks

Latest commit

 

History

32 Commits

Folders and files

Repository files navigation

TS-Rust

Rust-like Result<T, E> and Option<T> for TypeScript / Deno.

This library gives you expressive, type-safe error handling and optional values without leaning on null/undefined or raw try/catch.

Features

  • Result<T, E> — explicit success (Ok) or failure (Err)
  • Option<T> — explicit presence (Some) or absence (None)
  • Ergonomic factories — ok(), err(), some(), none() (preferred over new)
  • Fluent API — map, andThen, mapErr, unwrapOr, expect, match, etc.
  • Practical helpers — attempt / attemptAsync (and legacy tryCatch / asyncTryCatch aliases) to turn throwing functions into Result
  • Zero dependencies, strict TypeScript, works great in Deno

Installation & Usage

// Deno
import { attempt, err, none, ok, Result, some } from "jsr:@0xc0de666/ts-rust";

// or local import
// import { ok, err, some, none } from "./mod.ts";

Recommended style (factories):

const user = ok({ id: 1, name: "Ada" });
const missing = none<string>();
const error = err("Not found");

You can still use the classes directly if you prefer:

import { Err, None, Ok, Some } from "./mod.ts";

const user = new Ok({ id: 1 });

Quick Start

import { attempt, err, ok } from "./mod.ts";

// Happy path
const result = ok(42)
	.map((n) => n * 2)
	.andThen((n) => ok(`The answer is ${n}`));

console.log(result.unwrap()); // "The answer is 84"

// Error path
const failure = err("boom")
	.mapErr((e) => `Error: ${e}`);

console.log(failure.unwrapErr()); // "Error: boom"

// Convert throwing code
const parsed = attempt(() => JSON.parse('{"valid": true}'));
if (parsed.isOk()) {
	console.log("Parsed:", parsed.unwrap());
}

See the runnable examples in the examples/ directory:

  • examples/basic-result.ts
  • examples/basic-option.ts

Run them with:

deno run examples/basic-result.ts
deno run examples/basic-option.ts

API

Option<T>

Method Some<T> None<T>
isSome() true false
isNone() false true
unwrap() returns value throws
unwrapOr(default) returns value returns default
unwrapOrElse(fn) returns value calls fn
expect(msg) returns value throws with msg
map(fn) Some(fn(value)) None
andThen(fn) fn(value) None
or(other) this other
orElse(fn) this fn()
match(onSome, onNone) onSome(value) onNone()
flatten() inner Option (if nested) None

Factories

some(42); // Some<number>
none<number>(); // None<number>

Result<T, E>

Method Ok<T, E> Err<E, T>
isOk() true false
isErr() false true
unwrap() returns value throws
unwrapErr() throws returns error
unwrapOrElse(fn) returns value calls fn
expect(msg) returns value throws with msg
map(fn) Ok(fn(value)) Err(error)
mapErr(fn) Ok(value) Err(fn(error))
andThen(fn) fn(value) Err(error)
ok() Some(value) None
err() None Some(error)
match(onOk, onErr) onOk(value) onErr(error)

Factories

ok(42); // Ok<number>
err("something"); // Err<string>

Utility Functions

attempt (recommended)

Wraps a synchronous function that may throw and returns a Result.

const result = attempt(() => {
	// may throw
	return JSON.parse(input);
});

attemptAsync (recommended)

Wraps an async function that may reject and returns a Promise<Result>.

const result = await attemptAsync(async () => {
	const res = await fetch(url);
	if (!res.ok) throw new Error("HTTP " + res.status);
	return res.json();
});

Legacy aliases

For backward compatibility, the old names are still available as aliases (they will be removed in a future major version):

  • tryCatch → alias for attempt
  • asyncTryCatch → alias for attemptAsync

You can keep using them during migration:

const result = tryCatch(() => riskySync()); // still works
const result = await asyncTryCatch(() => riskyAsync()); // still works

Running Tests

deno test src/test.ts

License

MIT

About

Rust-like Result and Option Utilities for TypeScript

Resources

Stars

3 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages