@ts-rust/std
v0.4.0
Published
Rust-inspired utilities for TypeScript: Option, Result, and error handling for safer, more predictable code.
Maintainers
Readme
@ts-rust/std
A TypeScript library inspired by Rust's
Option<T> and
Result<T, E> types,
designed to bring type-safe, ergonomic, and robust value and error handling to
JavaScript projects. Built with TypeScript, @ts-rust/std ensures no runtime
errors are thrown by leveraging static typing and functional programming patterns.
This package is part of the @ts-rust monorepo, which adapts Rust's standard
library concepts into idiomatic TypeScript.
Installation
npm install @ts-rust/stdQuick Example
import { some, none, ok, err } from "@ts-rust/std";
// Option: handle missing values explicitly
function findUser(id: number) {
return id === 1 ? some("Alice") : none();
}
findUser(1).unwrapOr("Guest"); // "Alice"
findUser(2).unwrapOr("Guest"); // "Guest"
// Result: handle errors as values
function divide(a: number, b: number) {
return b !== 0 ? ok(a / b) : err("Division by zero");
}
divide(10, 2).unwrapOr(0); // 5
divide(10, 0).unwrapOr(0); // 0Documentation
For detailed documentation, visit krawitzzz.github.io/ts-rust.
Key Exports
Option<T>,some,none,fromNullable,fromUndefined: For handling optional values.Result<T, E>,ok,err,run,runAsync,fromPromise: For handling success/error scenarios.PendingOption<T>,pendingOption,pendingSome,pendingNone: Async optional values.PendingResult<T, E>,pendingResult,pendingOk,pendingErr: Async success/error handling.
Contributing
We welcome contributions! If you find a bug or have a feature request:
- Check the issue tracker to see if it's already reported.
- Open a new issue with a clear title, description, and reproduction steps (if applicable).
- For code contributions, fork the repo, create a branch, and submit a pull request.
Please follow the code of conduct when interacting with the project.
