FP-TS Option

Quick reference for FP-TS Option
import * as O = from('fp-ts/lib/Option');

const empty = O.none;
const withValue = O.some(1);

const fromNullableA = O.fromNullable(null); // none
const fromNullableB = O.fromNullable(1); // some(1)

const getOrElseA = O.getOrElse(empty, () => 'default'); // 'default'
const getOrElseB = O.getOrElse(withValue, () => 'default'); // 1

const mapA = O.map(empty, (x) => x + 1); // none
const mapB = O.map(withValue, (x) => x + 1); // some(2)

const flatMappedA = O.chain(withValue, (x) => O.some(x + 1)); // some(2)
const flatMappedB = O.chain(empty, (x) => O.some(x + 1)); // none

const toArrayA = O.toArray(empty); // []
const toArrayB = O.toArray(withValue); // [1]

const toNullableA = O.toNullable(empty); // null
const toNullableB = O.toNullable(withValue); // 1

const toUndefinedA = O.toUndefined(empty); // undefined
const toUndefinedB = O.toUndefined(withValue); // 1

const pipeA = pipe(
  withValue,
  O.map((x) => x + 1),
  O.getOrElse(() => 0),
); // 2

const pipeB = pipe(
  empty,
  O.map((x) => x + 1),
  O.getOrElse(() => 0),
); // 0