Skip to content

Option

Functions for working with Option<T> (Some(v) / None) values.

FunctionSignatureDescription
Option.mapOption<T>, (T) -> U -> Option<U>Transform the inner value if present
Option.flatMapOption<T>, (T) -> Option<U> -> Option<U>Chain option-returning operations
Option.unwrapOrOption<T>, T -> TExtract value or use default
Option.isSomeOption<T> -> booleanCheck if value is present
Option.isNoneOption<T> -> booleanCheck if value is absent
Option.toResultOption<T>, E -> Result<T, E>Convert to Result with error for None
Option.filterOption<T>, (T) -> boolean -> Option<T>Keep Some if predicate passes, else None
Option.orOption<T>, Option<T> -> Option<T>Return first Some, else second
Option.orElseOption<T>, () -> Option<T> -> Option<T>Lazy fallback chain
Option.valuesArray<Option<T>> -> Array<T>Extract all Some values, discard None
Option.mapOrOption<T>, U, (T) -> U -> UMap + default in one step
Option.flattenOption<Option<T>> -> Option<T>Unwrap nested Options
Option.zipOption<T>, Option<U> -> Option<(T, U)>Combine two Options into a tuple
Option.inspectOption<T>, (T) -> () -> Option<T>Side-effect without changing the value
Option.toErrOption<E> -> Result<(), E>Convert to Err if present (for { data, error } patterns)
Option.allArray<Option<T>> -> Option<Array<T>>Collect all Some values, None if any missing
Option.anyArray<Option<T>> -> Option<T>Return first Some, or None
Option.guardOption<T>, U, (T) -> U -> UBail with fallback on None, continue with unwrapped value (for use)
// Transform without unwrapping
const upper = user.nickname
|> Option.map((n) => String.toUpperCase(n))
// Some("RYAN") or None
// Chain lookups
const avatar = user.nickname
|> Option.flatMap((n) => findAvatar(n))
// Extract with fallback
const display = user.nickname
|> Option.unwrapOr(user.name)
// Lazy fallback chain
const config = localConfig
|> Option.orElse(() => envConfig)
|> Option.orElse(() => defaultConfig)
// Convert to Result for error handling
const name = user.nickname
|> Option.toResult("User has no nickname")
// Filter — keep Some only if predicate passes
const longName = user.nickname
|> Option.filter((n) => String.length(n) > 3)
// Zip — combine two Options
const pair = Option.zip(firstName, lastName)
// Some(("Alice", "Smith")) or None
// Handle { data, error } pattern (TanStack Query, Supabase, etc.)
const { data, error } = await supabase.rpc("get_entries", { query })
error |> Option.toErr? // bail if error exists
const rows = data |> Option.unwrapOr([])
// Collect all Options
const allNames = [Some("Alice"), Some("Bob"), None]
|> Option.all // None (one is missing)

Option.guard combines with use for early returns when a value is missing:

// Bail with fallback if None, continue with unwrapped value
use user <- Option.guard(maybeUser, <LoginPage />)
<ProfilePage user={user} />

See the Callback Flattening & Guards guide for the full pattern.