rusty-ts
v1.4.0
Published
Inspired by Rust's Option and Result.
Downloads
374
Maintainers
Readme
rusty-ts
Rust-inspired Option
and Result
types for TypeScript.
Usage
npm install --save rusty-ts
Option
import { Option, option } from "rusty-ts";
const a = option.some("foo");
const b = a.map(x => x.toUpperCase());
const c = option.some(3);
const d = a.andThen(a => c.map(c => a.repeat(c)));
function f(opt: Option<string>) {
console.log(opt.unwrap());
}
// Logs "FOOFOOFOO"
f(d);
Result
import { Result, result } from "rusty-ts";
const a = result.ok("foo");
const b = a.map(x => x.toUpperCase());
const c = result.err(3);
const d = c.orElse(cError => b.map(bVal => bVal.repeat(cError)));
function f(opt: Result<string>) {
console.log(opt.unwrap());
}
// Logs "FOOFOOFOO"
f(d);
Why Option
and option
(or Result
and result
)?
Option
is just an interface—any Option
-compatible code you write will be compatible with any implementation of Option
.
This gives you the flexibility to implement Option
however you like.
However, you probably don't want to write your own implementation, so we provide you with one out-of-the-box.
The option
object provides a namespace that groups the factories
of the default implementation.
To instantiate an Some or None variant, simply call option.some()
or option.none()
, respectively.
The same goes for Result
(the interface), and result
(the namespace containing the factory functions).
API Docs
Docs can be found here.