A Rust-inspired Option
type for TypeScript.
This package has been deprecated.
Use rusty-ts
instead.
npm install --save @kylejlin/option
import { 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);
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.
Docs can be found here.
Option
in a browser environmentNote: 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.
Generated using TypeDoc