Skip to main content
The Option type represents optional values. An Option<A> can either be Some<A> (containing a value) or None (representing absence of a value).

Type Definition

type Option<A> = None<A> | Some<A>

Constructors

none

Creates an Option representing the absence of a value.
const none: <A = never>() => Option<A>

some

Creates an Option containing a value.
const some: <A>(value: A) => Option<A>

fromNullable

Converts a nullable value to an Option.
const fromNullable: <A>(value: A | null | undefined) => Option<A>

Operations

map

Transforms the value inside an Option.
const map: <A, B>(f: (a: A) => B) => (self: Option<A>) => Option<B>

flatMap

Sequences computations that return Option values.
const flatMap: <A, B>(f: (a: A) => Option<B>) => (self: Option<A>) => Option<B>

getOrElse

Extracts the value from an Option or returns a default.
const getOrElse: <B>(onNone: LazyArg<B>) => <A>(self: Option<A>) => A | B

match

Pattern matches on an Option.
const match: <B, A, C = B>(options: {
  readonly onNone: LazyArg<B>
  readonly onSome: (a: A) => C
}) => (self: Option<A>) => B | C

Example

import { Option } from "effect"

const divide = (a: number, b: number): Option.Option<number> =>
  b === 0 ? Option.none() : Option.some(a / b)

const result = divide(10, 2).pipe(
  Option.map(n => n * 2),
  Option.getOrElse(() => 0)
)
console.log(result) // 10

Build docs developers (and LLMs) love