import { Option } from "https://deno.land/x/eitherway@0.10.0/lib/core/option.ts";
Functions
Use this to apply an | |
Alias for Option() | |
Use this if all falsy values should be evaluated to | |
Use this if instances of | |
Use this to return the provided instance of | |
Use this to compose functions and | |
Same as |
import { type Option } from "https://deno.land/x/eitherway@0.10.0/lib/core/option.ts";
Option
Option<T>
represents:
- EITHER the encapsulation of a value of type
<T>
viaSome<T>
- OR the absence of a value via
None
It's the composable equivalent of the union <T | undefined>
Not only is it useful for representing a value OR the absence of it, but also representing a value and communicating a certain fact about it. As in the following example:
import { Option } from "./option.ts";
type Prime = number;
declare function toPrime(n: number): Option<Prime>;
Furthermore, it's important to note that the encapsulated vaule must not
be nullish
It's impossible to create an instance of Some<null | undefined>
The namespace provides additional constructors when it's desired that
the return type is invariant over fallible (i.e. Error) or falsy types.
The namespace Options
provides a couple of collection helpers.
Examples
Example 1
Example 1
import { assert } from "./assert.ts";
import { Option, None, Some } from "./option.ts";
const str: string | undefined = "thing";
const undef: string | undefined = undefined;
const some: Option<string> = Option(str);
const none: Option<string> = Option(undef);
assert(some instanceof Option === true);
assert(none instanceof Option === true);
assert(some.isSome() === true);
assert(none.isNone() === true);
import { Option } from "https://deno.land/x/eitherway@0.10.0/lib/core/option.ts";