@kylejlin/option v2.0.1
Option
A Rust-inspired Option type for TypeScript.
DEPRECATION NOTICE
This package has been deprecated.
Use rusty-ts instead.
Usage
npm install --save @kylejlin/optionimport { Option, option } from "@kylejlin/option";
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);Why Option and option?
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 via the option factory object.
To instantiate a Some or None variant, simply call option.some() or option.none(), respectively.
API Docs
Docs can be found here.
Caveats
Forgetting to import Option in a browser environment
Note: You only have to worry about the following section if your tsconfig's lib includes "dom".
If you reference Option but forget to import it from this package, this will likely be a silent error instead of the expected Cannot find name 'Option'. (2304). This is because the browser declares an Option constructor as part of its DOM API.