-
Notifications
You must be signed in to change notification settings - Fork 0
Home
ghaerdi edited this page Aug 14, 2026
·
3 revisions
A TypeScript library inspired by Rust, providing Result and Option types for
safe error handling and null management with functional programming patterns.
JavaScript/TypeScript error handling often relies on try...catch blocks or
nullable return types, which can be verbose or hide potential errors. rustify
brings Rust-inspired monads like Result and Option to TypeScript, enabling
functional programming patterns for safer code. This allows you to:
-
Handle errors explicitly: Functions return a
Resultwhich is eitherOk(value)for success orErr(error)for failure. -
Manage nullable values safely: Use
Optionto represent values that may or may not exist, eliminating null/undefined errors. -
Improve type safety: Both
Result<T, E>andOption<T>types are tracked by the type system. -
Chain operations safely: Monadic methods like
andThen,map, andorElseallow elegant functional composition. -
Perform exhaustive checks: The
matchmethod ensures you handle all cases explicitly. -
Easily wrap unsafe functions:
Result.fromandOption.fromNullableprovide simple ways to convert potentially unsafe operations. -
Destructure results easily: Use
asTuple()for Go-style[err, val]destructuring, orasObject()if you prefer{ error, value }destructuring.
npm install @ghaerdi/rustify
# or
deno add @ghaerdi/rustifyimport { Err, None, Ok, Option, Result, Some } from "@ghaerdi/rustify";
function divide(numerator: number, denominator: number): Result<number, string> {
if (denominator === 0) return Err("Cannot divide by zero");
return Ok(numerator / denominator);
}
const result = divide(10, 2);
console.log(result.unwrap()); // 5- Installation — how to install with npm, yarn, pnpm, or JSR.
-
Option — full API reference for the
Option<T>monad. -
Result — full API reference for the
Result<T, E>monad. -
match — type-safe pattern matching with
match(). - Examples — worked examples for common patterns.
MIT License — see the LICENSE file in the repository for details.