Option
Functions for working with Option<T> (Some(v) / None) values.
Functions
Section titled “Functions”| Function | Signature | Description |
|---|---|---|
Option.map | Option<T>, (T) -> U -> Option<U> | Transform the inner value if present |
Option.flatMap | Option<T>, (T) -> Option<U> -> Option<U> | Chain option-returning operations |
Option.unwrapOr | Option<T>, T -> T | Extract value or use default |
Option.isSome | Option<T> -> boolean | Check if value is present |
Option.isNone | Option<T> -> boolean | Check if value is absent |
Option.toResult | Option<T>, E -> Result<T, E> | Convert to Result with error for None |
Option.filter | Option<T>, (T) -> boolean -> Option<T> | Keep Some if predicate passes, else None |
Option.or | Option<T>, Option<T> -> Option<T> | Return first Some, else second |
Option.orElse | Option<T>, () -> Option<T> -> Option<T> | Lazy fallback chain |
Option.values | Array<Option<T>> -> Array<T> | Extract all Some values, discard None |
Option.mapOr | Option<T>, U, (T) -> U -> U | Map + default in one step |
Option.flatten | Option<Option<T>> -> Option<T> | Unwrap nested Options |
Option.zip | Option<T>, Option<U> -> Option<(T, U)> | Combine two Options into a tuple |
Option.inspect | Option<T>, (T) -> () -> Option<T> | Side-effect without changing the value |
Option.toErr | Option<E> -> Result<(), E> | Convert to Err if present (for { data, error } patterns) |
Option.all | Array<Option<T>> -> Option<Array<T>> | Collect all Some values, None if any missing |
Option.any | Array<Option<T>> -> Option<T> | Return first Some, or None |
Option.guard | Option<T>, U, (T) -> U -> U | Bail with fallback on None, continue with unwrapped value (for use) |
Examples
Section titled “Examples”// Transform without unwrappingconst upper = user.nickname |> Option.map((n) => String.toUpperCase(n))// Some("RYAN") or None
// Chain lookupsconst avatar = user.nickname |> Option.flatMap((n) => findAvatar(n))
// Extract with fallbackconst display = user.nickname |> Option.unwrapOr(user.name)
// Lazy fallback chainconst config = localConfig |> Option.orElse(() => envConfig) |> Option.orElse(() => defaultConfig)
// Convert to Result for error handlingconst name = user.nickname |> Option.toResult("User has no nickname")
// Filter — keep Some only if predicate passesconst longName = user.nickname |> Option.filter((n) => String.length(n) > 3)
// Zip — combine two Optionsconst 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 existsconst rows = data |> Option.unwrapOr([])
// Collect all Optionsconst allNames = [Some("Alice"), Some("Bob"), None] |> Option.all // None (one is missing)Guard Pattern
Section titled “Guard Pattern”Option.guard combines with use for early returns when a value is missing:
// Bail with fallback if None, continue with unwrapped valueuse user <- Option.guard(maybeUser, <LoginPage />)<ProfilePage user={user} />See the Callback Flattening & Guards guide for the full pattern.