Module either

Functional unions and railway-oriented programming.

Remarks

Either<A, B> is a type that represents one of two values. It is represented by two variants: Left<A> and Right<B>.

  • A Left<A> is a left-sided Either and contains a value of type A.
  • A Right<B> is a right-sided Either and contains a value of type B.

The companion Either:namespace namespace provides utilities for working with the Either<A, B> type.

Handling failure

Either is also used to represent a value which is either a success or a failure. Left variant represents a failed value, while the Right variant represents a successful value.

Some combinators for Either are specialized for this failure-handling use case, and provide a right-biased behavior that "short-circuits" a computation on the first Left. This behavior allows functions that return Either to be composed in a way that propogates failures while applying logic to successes -- a useful feature for railway-oriented programming.

Using Either with promises

AsyncEither<A, B> is an alias for Promise<Either<A, B>>. The companion AsyncEither:namespace namespace provides utilities for working with the AsyncEither<A, B> type.

To accommodate promise-like values, this module also provides the AsyncEitherLike<A, B> type as an alias for PromiseLike<Either<A, B>>.

Importing from this module

The types and namespaces from this module can be imported under the same aliases:

import { AsyncEither, Either } from "@neotype/prelude/either.js";

Or, they can be imported and aliased separately:

import {
type AsyncEither,
type Either,
AsyncEither as AE,
Either as E
} from "@neotype/prelude/either.js";

Index

Namespaces

Type Aliases

Generated using TypeDoc