<A>(collection: Iterable<A>): Option<A>Wraps the first element of an Iterable in a Some, or returns None if
the iterable is empty.
When to use
Use when you need to safely extract the head of a collection, including generators or lazy iterables.
Details
- Only consumes the first element; does not iterate the rest
- Returns
Nonefor empty iterables
Example (Getting the first element)
import { Option } from "effect"
console.log(Option.fromIterable([1, 2, 3]))
// Output: { _id: 'Option', _tag: 'Some', value: 1 }
console.log(Option.fromIterable([]))
// Output: { _id: 'Option', _tag: 'None' }export const const fromIterable: <A>(
collection: Iterable<A>
) => Option<A>
Wraps the first element of an Iterable in a Some, or returns None if
the iterable is empty.
When to use
Use when you need to safely extract the head of a collection, including
generators or lazy iterables.
Details
- Only consumes the first element; does not iterate the rest
- Returns
None for empty iterables
Example (Getting the first element)
import { Option } from "effect"
console.log(Option.fromIterable([1, 2, 3]))
// Output: { _id: 'Option', _tag: 'Some', value: 1 }
console.log(Option.fromIterable([]))
// Output: { _id: 'Option', _tag: 'None' }
fromIterable = <function (type parameter) A in <A>(collection: Iterable<A>): Option<A>A>(collection: Iterable<A>collection: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A>(collection: Iterable<A>): Option<A>A>): type Option<A> = None<A> | Some<A>The Option data type represents optional values. An Option<A> is either
Some<A>, containing a value of type A, or None, representing absence.
When to use
Use to represent initial values that may not yet exist
- Returning from partial functions (not defined for all inputs)
- Managing optional fields in data structures
Namespace containing utility types for Option.
When to use
Use to access type-level helpers associated with Option.
Option<function (type parameter) A in <A>(collection: Iterable<A>): Option<A>A> => {
for (const const a: Aa of collection: Iterable<A>collection) {
return const some: <A>(value: A) => Option<A>Wraps the given value into an Option to represent its presence.
When to use
Use to wrap a known present value as Option
- Returning a successful result from a partial function
Details
- Always returns
Some<A>
- Does not filter
null or undefined; use
fromNullishOr
for that
Example (Wrapping a value)
import { Option } from "effect"
// ┌─── Option<number>
// ▼
const value = Option.some(1)
console.log(value)
// Output: { _id: 'Option', _tag: 'Some', value: 1 }
some(const a: Aa)
}
return const none: <A = never>() => Option<A>Creates an Option representing the absence of a value.
When to use
Use to represent a missing or uninitialized value, such as returning "no
result" from a function.
Details
- Returns
Option<never>, which is a subtype of Option<A> for any A
- Always returns the same singleton instance
Example (Creating an empty Option)
import { Option } from "effect"
// ┌─── Option<never>
// ▼
const noValue = Option.none()
console.log(noValue)
// Output: { _id: 'Option', _tag: 'None' }
none()
}