The Option data type represents optional values, providing a type-safe alternative to null or undefined.
Type Signature
type Option<A> = Some<A> | None<A>
interface Some<A> {
readonly _tag: "Some"
readonly value: A
}
interface None<A> {
readonly _tag: "None"
}
Creating Options
import { Option } from "effect"
const value = Option.some(42)
// Option<number>
console.log(value)
// { _id: 'Option', _tag: 'Some', value: 42 }
import { Option } from "effect"
const noValue = Option.none()
// Option<never>
console.log(noValue)
// { _id: 'Option', _tag: 'None' }
import { Option } from "effect"
const value1 = Option.fromNullable(42) // Some(42)
const value2 = Option.fromNullable(null) // None
Type Guards
import { Option } from "effect"
const value = Option.some(1)
if (Option.isSome(value)) {
console.log(value.value) // 1
}
if (Option.isNone(value)) {
console.log("No value")
}
Pattern Matching
match
Handle both Some and None cases.
import { Option } from "effect"
const foo = Option.some(1)
const message = Option.match(foo, {
onNone: () => "Option is empty",
onSome: (value) => `Option has a value: ${value}`
})
console.log(message)
// "Option has a value: 1"
getOrElse
getOrNull
getOrUndefined
import { Option } from "effect"
const value = Option.some(1).pipe(
Option.getOrElse(() => 0)
)
// 1
const none = Option.none().pipe(
Option.getOrElse(() => 0)
)
// 0
import { Option } from "effect"
Option.getOrNull(Option.some(1)) // 1
Option.getOrNull(Option.none()) // null
import { Option } from "effect"
Option.getOrUndefined(Option.some(1)) // 1
Option.getOrUndefined(Option.none()) // undefined
map
import { Option } from "effect"
const result = Option.some(2).pipe(
Option.map(n => n * 2)
)
// Some(4)
flatMap
import { Option } from "effect"
const result = Option.some(2).pipe(
Option.flatMap(n => n > 0 ? Option.some(n * 2) : Option.none())
)
// Some(4)
filter
import { Option } from "effect"
const result = Option.some(5).pipe(
Option.filter(n => n > 3)
)
// Some(5)
const filtered = Option.some(2).pipe(
Option.filter(n => n > 3)
)
// None
Conversions
toArray
fromIterable
getRight
import { Option } from "effect"
Option.toArray(Option.some(1)) // [1]
Option.toArray(Option.none()) // []
import { Option } from "effect"
Option.fromIterable([1, 2, 3]) // Some(1)
Option.fromIterable([]) // None
import { Either, Option } from "effect"
Option.getRight(Either.right("ok")) // Some("ok")
Option.getRight(Either.left("err")) // None
Combining Options
zipWith
import { Option } from "effect"
const result = Option.zipWith(
Option.some(2),
Option.some(3),
(a, b) => a + b
)
// Some(5)
orElse
import { Option } from "effect"
const result = Option.none().pipe(
Option.orElse(() => Option.some(42))
)
// Some(42)
Option is ideal for representing values that may or may not exist, eliminating null pointer errors and making optionality explicit in your type signatures.
Common Use Cases
- Initial values for configurations
- Optional fields in data structures
- Results from partial functions (functions not defined for all inputs)
- Safe null handling without exceptions
Key Operations
| Operation | Description |
|---|
some | Creates an Option containing a value |
none | Creates an empty Option |
fromNullable | Converts nullable values to Option |
isSome | Checks if Option contains a value |
isNone | Checks if Option is empty |
match | Pattern match on Option |
getOrElse | Extracts value or returns default |
map | Transforms the contained value |
flatMap | Chains Option-returning operations |
filter | Filters based on a predicate |