Skip to content
ghaerdi edited this page Aug 14, 2026 · 2 revisions

Option<T>

Option<T> is a discriminated union — every value exposes a literal __tag: "some" or "none" — so you can narrow with if (opt.__tag === "some") (or isSome() / isNone()).

Checking

  • isSome(): Returns true if Some.
  • isNone(): Returns true if None.
  • isSomeAnd(fn): Returns true if Some and the value satisfies fn.
  • contains(value): Returns true if Some and the value equals value.

Extracting Values

  • unwrap(): Returns the Some value, throws if None. Use with caution.
  • expect(message): Returns the Some value, throws message if None.
  • unwrapOr(defaultValue): Returns the Some value or defaultValue if None.
  • unwrapOrElse(fn): Returns the Some value or computes default using fn() if None.
  • unwrapOrDefault(): Returns the Some value or throws (no Default trait in TypeScript).

Mapping & Transformation

  • map(fn): Maps Some<T> to Some<U>. Leaves None untouched.
  • mapOr(defaultValue, fn): Applies fn to Some value, returns defaultValue if None.
  • mapOrElse(defaultFn, fn): Applies fn to Some value, applies defaultFn if None.
  • mapOrDefault(defaultValue, fn): Applies fn to Some value, returns defaultValue if None.

Chaining & Side Effects

  • and(res): Returns res if Some, else returns None.
  • andThen(fn): Calls fn(someValue) if Some, returns the resulting Option.
  • or(res): Returns self if Some, else returns res.
  • orElse(fn): Returns self if Some, else calls fn() and returns the result.
  • xor(other): Returns Some if exactly one of self or other is Some, else None.
  • inspect(fn): Calls fn(someValue) if Some, returns original Option.

Filtering

  • filter(predicate): Returns Some(value) if Some and predicate passes, else None.

Flattening & Transposing

  • flatten(): Converts Option<Option<T>> to Option<T>.
  • transpose(): Transposes Option<Result<T, E>> into Result<Option<T>, E>.

Inserting & Taking (Mutating)

  • getOrInsert(value): Returns the contained value. If None, inserts and returns value.
  • getOrInsertWith(fn): Returns the contained value. If None, computes and inserts fn().
  • take(): Extracts the value, leaving the option as None. Returns the value as Some.
  • takeIf(predicate): Extracts the value if Some and predicate passes, leaving None.

Pattern Matching

  • match(matcher): Executes matcher.Some(value) or matcher.None(), returning the result.

Cloning

  • cloned(): Returns a new Option with a deep clone of the Some value (using structuredClone).

Zipping

  • zip(other): Zips Some(a) with Some(b) into Some([a, b]), else None.
  • zipWith(other, fn): Zips Some(a) with Some(b) using fn(a, b) into Some(result), else None.

Iteration

  • [Symbol.iterator](): Iterator protocol — yields the Some value if it is iterable.

Converting to Result

  • okOr(err): Converts Some(v) to Ok(v), None to Err(err).
  • okOrElse(fn): Converts Some(v) to Ok(v), None to Err(fn()).

Static Methods on Option

  • Option.fromNullable(fn): Wraps a function that might return null or undefined. Returns Some(value) or None.
  • Option.isOption(value): Type guard, returns true if value is Some or None.

See Also

Clone this wiki locally