rust-types
is a TypeScript library providing Rust-like Result and Option types
for handling success or failure and optional values, respectively.
I tested many of these libraries, and my personal conclusion is that https://github.com/vultix/ts-results have the best api in the context/limitation of typescript. That library haven't been updated in a while (also latest version didn't work for me) so it was a good execuse to add my own.
The Result type represents either success with a value or failure with an error.
import { Err, Ok, Result } from "jsr:@sigmasd/rust-types/result";
const result = Ok(41); // Result with value 41
const errorResult = Err("Something went wrong"); // Result with error
The Option type represents an optional value that may or may not exist.
import { Option } from "jsr:@sigmasd/rust-types/option";
const option = Option.wrap(41); // Option with value 41
const emptyOption = Option.wrap(null); // Empty Option
Check out the tests for examples.