Skip to main content
Module

x/eitherway/mod.ts>Option

Yet Another Option and Result Implementation - providing safe abstractions for fallible flows inspired by F# and Rust
Latest
namespace Option
import { Option } from "https://deno.land/x/eitherway@0.10.0/mod.ts";

Functions

Use this to apply an Option<T> to a handler of type Option<MapFn>

Alias for Option()

Use this if all falsy values should be evaluated to None

Use this if instances of Error should be evaluated to None

Use this to return the provided instance of Option<T> Mostly usefull for flattening or en lieu of a no-op

Use this to compose functions and Option constructors

Same as Option.lift but with a safety net.

type alias Option
import { type Option } from "https://deno.land/x/eitherway@0.10.0/mod.ts";

Option

Option<T> represents:

  • EITHER the encapsulation of a value of type <T> via Some<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

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);
definition: Some<T> | None
function Option
import { Option } from "https://deno.land/x/eitherway@0.10.0/mod.ts";