JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 35
  • Score
    100M100P100Q67306F
  • License MIT

Inspired by Rust's Option and Result.

Package Exports

  • rusty-ts

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (rusty-ts) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

rusty-ts

Build Status Coverage Status npm version Downloads

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.