Type implementations for TypeScript inspired by Rust. They provide a typed way
to handle success, errors, and optional values without throwing, while still
giving you escape hatches like unwrap and expect when needed. Just don't use
unwrap in production code!
- Result
- Option
findOption(equivalent to Rust'sfind, returningOptioninstead ofundefined)
Constructors are functions, and every operation is available as a method on the
returned Result or Option. The API has no external dependencies.
Result:Ok(value),Err(error), also available asResult.Ok(value),Result.Err(error)Option:Some(value),None(), also available asOption.Some(value),Option.None()
Result:isOk,isErr,isOkAnd,isErrAnd,matchOption:isSome,isNone,isSomeAnd,isNoneOr,match
The type guards narrow the variant, so value or error can be accessed safely.
match handles both variants and returns the selected branch's value.
Result:map,mapErr,mapOr,mapOrElse,flattenOption:map,mapOr,mapOrElse,filter,zip,zipWith,flatten
Result:and,andThen,or,orElseOption:and,andThen,or,orElse
Result:unwrap,expect,unwrapOr,unwrapOrElseOption:unwrap,expect,unwrapOr,unwrapOrElse
unwrap and expect throw when called on Err or None; prefer exhaustive
handling or a fallback in production code.
Result:inspect,inspectErrOption:inspect
To install all extended methods eagerly as a side effect, import:
import "lrustlike/extensions/eager";To install all extended methods lazily, import and call the installer:
import { installIterableMethods } from "lrustlike/extensions/lazy";
installIterableMethods();Extensions can also be installed for a specific interface. Replace
<interface> with the lowercase interface name used in the path, and replace
<Interface> with its PascalCase name in the installer function. For example,
the Iterable interface uses iterable in import paths and Iterable in
function names.
Importing the eager module installs every extension for that interface as a side effect:
import "lrustlike/extensions/iterable/eager";The path follows this pattern:
lrustlike/extensions/<interface>/eager
Import the interface installer from its lazy module and call it explicitly:
import { installIterableMethods } from "lrustlike/extensions/iterable/lazy";
installIterableMethods();The import and installer names follow these patterns:
lrustlike/extensions/<interface>/lazy
install<Interface>Methods
The lazy interface module also exports installers for individual methods. The
installer name follows the install<MethodName>Method pattern. For example, to
install only findOption:
import { installFindOptionMethod } from "lrustlike/extensions/iterable/lazy";
installFindOptionMethod();import { type Result, Ok, Err } from "lrustlike/result";
function parseIntResult(s: string): Result<number, string> {
const n = Number.parseInt(s, 10);
return Number.isNaN(n) ? Err(`'${s}' is not a number`) : Ok(n);
}
const r = parseIntResult("42");
if (r.isOk()) {
console.log("value:", r.value);
} else {
console.log("error:", r.error);
}Using match for exhaustive handling:
const msg = parseIntResult("100").match({
Ok: v => `valid: ${v}`,
Err: e => `invalid: ${e}`,
});Using expect and unwrap (not recommended for production):
const x = parseIntResult("10").expect("expected an int");
const y = parseIntResult("oops").unwrap();Using isOkAnd and isErrAnd:
const isPositive = parseIntResult("7").isOkAnd(value => value > 0);
const isInvalidNumber = parseIntResult("abc").isErrAnd(error =>
error.includes("not a number"),
);import { type Option, Some, None } from "lrustlike/option";
function findUserName(id: number): Option<string> {
return id === 1 ? Some("Ferris") : None();
}
const name = findUserName(1);
if (name.isSome()) {
console.log("user:", name.value);
} else {
console.log("user not found");
}Using match for exhaustive handling:
const greeting = findUserName(1).match({
Some: name => `Hello, ${name}!`,
None: () => "Hello, stranger!",
});Transforming a value and providing a fallback:
const upperName = findUserName(1).map(name => name.toUpperCase());
const displayName = findUserName(2).unwrapOr("Anonymous");