Skip to main content Link Search Menu Expand Document (external link)

Option.ts overview

Extension to the effect/Option module providing interoperability with nullable values and JavaScript iterators.

Mental model

  • Option.Option<A> is a sum of Some<A> and None.
  • This module focuses on bridging Option with non-effect-aware shapes (nullable values and raw iterators).

Common tasks

  • Bridge with nullables: OptionOrNullable, fromOptionOrNullable
  • Bridge with iterators: fromNextIteratorValue

Quickstart

Example (Bridging from nullable input)

import * as MOption from "@parischap/effect-lib/MOption"

console.log(MOption.fromOptionOrNullable(null)) // None
console.log(MOption.fromOptionOrNullable(undefined)) // None
console.log(MOption.fromOptionOrNullable(42)) // Some(42)

Exports Grouped by Category


Models

OptionOrNullable (type alias)

Tagged union representing “an optional A” using whichever shape callers find natural: an Option<A>, a bare A, or null/undefined.

  • Use at API boundaries that accept input from code unaware of Option.

Signature

type OptionOrNullable<A> = Option.Option<A> | null | undefined | A

Source

Type (type alias)

Type on which this module’s functions operate.

Signature

type Type<A> = Option.Option<A>

Source

Utils

fromNextIteratorValue

Advances iterator by one step and lifts the result into an Option.

  • Returns Option.some(value) when the iterator produced a value, Option.none when it is exhausted.

Example (Pull next value from an iterator)

import * as MOption from "@parischap/effect-lib/MOption"

const it = [1, 2][Symbol.iterator]()
console.log(MOption.fromNextIteratorValue(it)) // Some(1)
console.log(MOption.fromNextIteratorValue(it)) // Some(2)
console.log(MOption.fromNextIteratorValue(it)) // None

Signature

declare const fromNextIteratorValue: <A>(iterator: Iterator<A>) => Option.Option<A>

Source

fromOptionOrNullable

Normalizes an OptionOrNullable to an Option.

  • When the input is already an Option, it is returned as-is.
  • null and undefined become Option.none; any other value becomes Option.some.

Example (Normalize varied inputs)

import { Option } from "effect"
import * as MOption from "@parischap/effect-lib/MOption"

console.log(MOption.fromOptionOrNullable(null)) // None
console.log(MOption.fromOptionOrNullable(undefined)) // None
console.log(MOption.fromOptionOrNullable(42)) // Some(42)
console.log(MOption.fromOptionOrNullable(Option.some(7))) // Some(7)

Signature

declare const fromOptionOrNullable: <A>(a: OptionOrNullable<A>) => Option.Option<A>

Source