<A, B>(f: (a: A) => Option<B>): (self: Option<A>) => Option<B>
<A, B>(self: Option<A>, f: (a: A) => Option<B>): Option<B>Applies a function that returns an Option to the value of a Some,
flattening the result. Returns None if the input is None.
When to use
Use when you need to chain dependent Option computations where each step
may return None.
Details
Some→ appliesfto the value and returns itsOptionresultNone→ returnsNonewithout callingf- Equivalent to
mapfollowed by flatten
Example (Chaining optional lookups)
import { Option } from "effect"
interface User {
readonly name: string
readonly address: Option.Option<{ readonly street: Option.Option<string> }>
}
const user: User = {
name: "John",
address: Option.some({ street: Option.some("123 Main St") })
}
const street = user.address.pipe(
Option.flatMap((addr) => addr.street)
)
console.log(street)
// Output: { _id: 'Option', _tag: 'Some', value: '123 Main St' }export const const flatMap: {
<A, B>(f: (a: A) => Option<B>): (
self: Option<A>
) => Option<B>
<A, B>(
self: Option<A>,
f: (a: A) => Option<B>
): Option<B>
}
Applies a function that returns an Option to the value of a Some,
flattening the result. Returns None if the input is None.
When to use
Use when you need to chain dependent Option computations where each step
may return None.
Details
Some → applies f to the value and returns its Option result
None → returns None without calling f
- Equivalent to
map followed by
flatten
Example (Chaining optional lookups)
import { Option } from "effect"
interface User {
readonly name: string
readonly address: Option.Option<{ readonly street: Option.Option<string> }>
}
const user: User = {
name: "John",
address: Option.some({ street: Option.some("123 Main St") })
}
const street = user.address.pipe(
Option.flatMap((addr) => addr.street)
)
console.log(street)
// Output: { _id: 'Option', _tag: 'Some', value: '123 Main St' }
flatMap: {
<function (type parameter) A in <A, B>(f: (a: A) => Option<B>): (self: Option<A>) => Option<B>A, function (type parameter) B in <A, B>(f: (a: A) => Option<B>): (self: Option<A>) => Option<B>B>(f: (a: A) => Option<B>f: (a: Aa: function (type parameter) A in <A, B>(f: (a: A) => Option<B>): (self: Option<A>) => Option<B>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) B in <A, B>(f: (a: A) => Option<B>): (self: Option<A>) => Option<B>B>): (self: Option<A>self: 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, B>(f: (a: A) => Option<B>): (self: Option<A>) => Option<B>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) B in <A, B>(f: (a: A) => Option<B>): (self: Option<A>) => Option<B>B>
<function (type parameter) A in <A, B>(self: Option<A>, f: (a: A) => Option<B>): Option<B>A, function (type parameter) B in <A, B>(self: Option<A>, f: (a: A) => Option<B>): Option<B>B>(self: Option<A>self: 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, B>(self: Option<A>, f: (a: A) => Option<B>): Option<B>A>, f: (a: A) => Option<B>f: (a: Aa: function (type parameter) A in <A, B>(self: Option<A>, f: (a: A) => Option<B>): Option<B>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) B in <A, B>(self: Option<A>, f: (a: A) => Option<B>): Option<B>B>): 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) B in <A, B>(self: Option<A>, f: (a: A) => Option<B>): Option<B>B>
} = import dualdual(
2,
<function (type parameter) A in <A, B>(self: Option<A>, f: (a: A) => Option<B>): Option<B>A, function (type parameter) B in <A, B>(self: Option<A>, f: (a: A) => Option<B>): Option<B>B>(self: Option<A>self: 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, B>(self: Option<A>, f: (a: A) => Option<B>): Option<B>A>, f: (a: A) => Option<B>f: (a: Aa: function (type parameter) A in <A, B>(self: Option<A>, f: (a: A) => Option<B>): Option<B>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) B in <A, B>(self: Option<A>, f: (a: A) => Option<B>): Option<B>B>): 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) B in <A, B>(self: Option<A>, f: (a: A) => Option<B>): Option<B>B> => const isNone: <A>(
self: Option<A>
) => self is None<A>
Checks whether an Option is None (absent).
When to use
Use when you need to branch on an absent Option before accessing .value.
Details
- Acts as a type guard, narrowing to
None<A>
Example (Checking for None)
import { Option } from "effect"
console.log(Option.isNone(Option.some(1)))
// Output: false
console.log(Option.isNone(Option.none()))
// Output: true
isNone(self: Option<A>self) ? 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() : f: (a: A) => Option<B>f(self: Option<A>(parameter) self: {
_tag: "Some";
_op: "Some";
value: A;
valueOrUndefined: A;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
self.Some<A>.value: Avalue)
)