Skip to main content
Module

x/eitherway/mod.ts>Options.all

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

Use this to transpose Option<T>[] to Some<T[]> if all elements are Some<T>

If one element is None, or if the input array/tuple is empty, None is immediately returned

This function retains type constraints like readonly on the input array or tuple and is able to infer variadic tuples

Examples

Example 1

import { assert } from "./assert.ts";
import { Option, Options, None, Some } from "./option.ts";

type StrictTuple = Readonly<[string, number, boolean]>;
const tuple = [
  Option("some" as string),
  Option(1 as number),
  Option(true as boolean),
] as const;
const empty: Option<string>[] = [];
const encode = JSON.stringify;

const someTuple: Option<StrictTuple> = Options.all(tuple);
const emptyIsNone : Option<string[]> = Options.all(empty);

if (someTuple.isNone() || emptyIsNone.isSome()) {
  throw TypeError("Unreachable in this example");
}

const unwrapped: StrictTuple = someTuple.unwrap();
const undef: undefined = emptyIsNone.unwrap();

assert(someTuple.isSome() === true);
assert(emptyIsNone.isNone() === true);
assert(encode(unwrapped) === encode(tuple));
assert(undef === undefined);

Type Parameters

O extends ReadonlyArray<Option<unknown>>

Returns

Option<InferredSomeTuple<O>>