/**
* @since 2.0.0
*/
import type { Either } from "./Either.js";
import * as Equivalence from "./Equivalence.js";
import type { LazyArg } from "./Function.js";
import type { TypeLambda } from "./HKT.js";
import type { Inspectable } from "./Inspectable.js";
import type { Order } from "./Order.js";
import type { Pipeable } from "./Pipeable.js";
import type { Predicate, Refinement } from "./Predicate.js";
import type { Covariant, NoInfer, NotFunction } from "./Types.js";
import type * as Unify from "./Unify.js";
import * as Gen from "./Utils.js";
/**
* The `Option` data type represents optional values. An `Option` can either
* be `Some`, containing a value of type `A`, or `None`, representing the
* absence of a value.
*
* **When to Use**
*
* You can use `Option` in scenarios like:
*
* - Using it for initial values
* - Returning values from functions that are not defined for all possible
* inputs (referred to as “partial functions”)
* - Managing optional fields in data structures
* - Handling optional function arguments
*
* @category Models
* @since 2.0.0
*/
export type Option = None | Some;
/**
* @category Symbols
* @since 2.0.0
*/
export declare const TypeId: unique symbol;
/**
* @category Symbols
* @since 2.0.0
*/
export type TypeId = typeof TypeId;
/**
* @category Models
* @since 2.0.0
*/
export interface None extends Pipeable, Inspectable {
readonly _tag: "None";
readonly _op: "None";
readonly [TypeId]: {
readonly _A: Covariant;
};
[Unify.typeSymbol]?: unknown;
[Unify.unifySymbol]?: OptionUnify;
[Unify.ignoreSymbol]?: OptionUnifyIgnore;
}
/**
* @category Models
* @since 2.0.0
*/
export interface Some extends Pipeable, Inspectable {
readonly _tag: "Some";
readonly _op: "Some";
readonly value: A;
readonly [TypeId]: {
readonly _A: Covariant;
};
[Unify.typeSymbol]?: unknown;
[Unify.unifySymbol]?: OptionUnify;
[Unify.ignoreSymbol]?: OptionUnifyIgnore;
}
/**
* @category Models
* @since 2.0.0
*/
export interface OptionUnify {
Option?: () => A[Unify.typeSymbol] extends Option | infer _ ? Option : never;
}
/**
* @since 2.0.0
*/
export declare namespace Option {
/**
* Extracts the type of the value contained in an `Option`.
*
* **Example** (Getting the Value Type of an Option)
*
* ```ts
* import { Option } from "effect"
*
* // Declare an Option holding a string
* declare const myOption: Option.Option
*
* // Extract the type of the value within the Option
* //
* // ┌─── string
* // ▼
* type MyType = Option.Option.Value
* ```
*
* @since 2.0.0
* @category Type-level Utils
*/
type Value> = [T] extends [Option] ? _A : never;
}
/**
* @category Models
* @since 2.0.0
*/
export interface OptionUnifyIgnore {
}
/**
* @category Type Lambdas
* @since 2.0.0
*/
export interface OptionTypeLambda extends TypeLambda {
readonly type: Option;
}
/**
* Represents the absence of a value by creating an empty `Option`.
*
* `Option.none` returns an `Option`, which is a subtype of `Option`.
* This means you can use it in place of any `Option` regardless of the type
* `A`.
*
* **Example** (Creating an Option with No Value)
*
* ```ts
* import { Option } from "effect"
*
* // An Option holding no value
* //
* // ┌─── Option
* // ▼
* const noValue = Option.none()
*
* console.log(noValue)
* // Output: { _id: 'Option', _tag: 'None' }
* ```
*
* @see {@link some} for the opposite operation.
*
* @category Constructors
* @since 2.0.0
*/
export declare const none: () => Option;
/**
* Wraps the given value into an `Option` to represent its presence.
*
* **Example** (Creating an Option with a Value)
*
* ```ts
* import { Option } from "effect"
*
* // An Option holding the number 1
* //
* // ┌─── Option
* // ▼
* const value = Option.some(1)
*
* console.log(value)
* // Output: { _id: 'Option', _tag: 'Some', value: 1 }
* ```
*
* @see {@link none} for the opposite operation.
*
* @category Constructors
* @since 2.0.0
*/
export declare const some: (value: A) => Option;
/**
* Determines whether the given value is an `Option`.
*
* **Details**
*
* This function checks if a value is an instance of `Option`. It returns `true`
* if the value is either `Option.some` or `Option.none`, and `false` otherwise.
* This is particularly useful when working with unknown values or when you need
* to ensure type safety in your code.
*
* @example
* ```ts
* import { Option } from "effect"
*
* console.log(Option.isOption(Option.some(1)))
* // Output: true
*
* console.log(Option.isOption(Option.none()))
* // Output: true
*
* console.log(Option.isOption({}))
* // Output: false
* ```
*
* @category Guards
* @since 2.0.0
*/
export declare const isOption: (input: unknown) => input is Option;
/**
* Checks whether an `Option` represents the absence of a value (`None`).
*
* @example
* ```ts
* import { Option } from "effect"
*
* console.log(Option.isNone(Option.some(1)))
* // Output: false
*
* console.log(Option.isNone(Option.none()))
* // Output: true
* ```
*
* @see {@link isSome} for the opposite check.
*
* @category Guards
* @since 2.0.0
*/
export declare const isNone: (self: Option) => self is None;
/**
* Checks whether an `Option` contains a value (`Some`).
*
* @example
* ```ts
* import { Option } from "effect"
*
* console.log(Option.isSome(Option.some(1)))
* // Output: true
*
* console.log(Option.isSome(Option.none()))
* // Output: false
* ```
*
* @see {@link isNone} for the opposite check.
*
* @category Guards
* @since 2.0.0
*/
export declare const isSome: (self: Option) => self is Some;
/**
* Performs pattern matching on an `Option` to handle both `Some` and `None`
* cases.
*
* **Details**
*
* This function allows you to match against an `Option` and handle both
* scenarios: when the `Option` is `None` (i.e., contains no value), and when
* the `Option` is `Some` (i.e., contains a value). It executes one of the
* provided functions based on the case:
*
* - If the `Option` is `None`, the `onNone` function is executed and its result
* is returned.
* - If the `Option` is `Some`, the `onSome` function is executed with the
* contained value, and its result is returned.
*
* This function provides a concise and functional way to handle optional values
* without resorting to `if` or manual checks, making your code more declarative
* and readable.
*
* **Example** (Pattern Matching with Option)
*
* ```ts
* 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)
* // Output: "Option has a value: 1"
* ```
*
* @category Pattern matching
* @since 2.0.0
*/
export declare const match: {
/**
* Performs pattern matching on an `Option` to handle both `Some` and `None`
* cases.
*
* **Details**
*
* This function allows you to match against an `Option` and handle both
* scenarios: when the `Option` is `None` (i.e., contains no value), and when
* the `Option` is `Some` (i.e., contains a value). It executes one of the
* provided functions based on the case:
*
* - If the `Option` is `None`, the `onNone` function is executed and its result
* is returned.
* - If the `Option` is `Some`, the `onSome` function is executed with the
* contained value, and its result is returned.
*
* This function provides a concise and functional way to handle optional values
* without resorting to `if` or manual checks, making your code more declarative
* and readable.
*
* **Example** (Pattern Matching with Option)
*
* ```ts
* 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)
* // Output: "Option has a value: 1"
* ```
*
* @category Pattern matching
* @since 2.0.0
*/
(options: {
readonly onNone: LazyArg;
readonly onSome: (a: A) => C;
}): (self: Option) => B | C;
/**
* Performs pattern matching on an `Option` to handle both `Some` and `None`
* cases.
*
* **Details**
*
* This function allows you to match against an `Option` and handle both
* scenarios: when the `Option` is `None` (i.e., contains no value), and when
* the `Option` is `Some` (i.e., contains a value). It executes one of the
* provided functions based on the case:
*
* - If the `Option` is `None`, the `onNone` function is executed and its result
* is returned.
* - If the `Option` is `Some`, the `onSome` function is executed with the
* contained value, and its result is returned.
*
* This function provides a concise and functional way to handle optional values
* without resorting to `if` or manual checks, making your code more declarative
* and readable.
*
* **Example** (Pattern Matching with Option)
*
* ```ts
* 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)
* // Output: "Option has a value: 1"
* ```
*
* @category Pattern matching
* @since 2.0.0
*/
(self: Option, options: {
readonly onNone: LazyArg;
readonly onSome: (a: A) => C;
}): B | C;
};
/**
* Converts an `Option`-returning function into a type guard.
*
* **Details**
*
* This function transforms a function that returns an `Option` into a type
* guard, ensuring type safety when validating or narrowing types. The returned
* type guard function checks whether the input satisfies the condition defined
* in the original `Option`-returning function.
*
* If the original function returns `Option.some`, the type guard evaluates to
* `true`, confirming the input is of the desired type. If the function returns
* `Option.none`, the type guard evaluates to `false`.
*
* This utility is especially useful for validating types in union types,
* filtering arrays, or ensuring safe handling of specific subtypes.
*
* @example
* ```ts
* import { Option } from "effect"
*
* type MyData = string | number
*
* const parseString = (data: MyData): Option.Option =>
* typeof data === "string" ? Option.some(data) : Option.none()
*
* // ┌─── (a: MyData) => a is string
* // ▼
* const isString = Option.toRefinement(parseString)
*
* console.log(isString("a"))
* // Output: true
*
* console.log(isString(1))
* // Output: false
* ```
*
* @category Conversions
* @since 2.0.0
*/
export declare const toRefinement: (f: (a: A) => Option) => (a: A) => a is B;
/**
* Converts an `Iterable` into an `Option`, wrapping the first element if it
* exists.
*
* **Details**
*
* This function takes an `Iterable` (e.g., an array, a generator, or any object
* implementing the `Iterable` interface) and returns an `Option` based on its
* content:
*
* - If the `Iterable` contains at least one element, the first element is
* wrapped in a `Some` and returned.
* - If the `Iterable` is empty, `None` is returned, representing the absence of
* a value.
*
* This utility is useful for safely handling collections that might be empty,
* ensuring you explicitly handle both cases where a value exists or doesn't.
*
* @example
* ```ts
* 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' }
* ```
*
* @category Constructors
* @since 2.0.0
*/
export declare const fromIterable: (collection: Iterable) => Option;
/**
* Converts an `Either` into an `Option` by discarding the error and extracting
* the right value.
*
* **Details**
*
* This function takes an `Either` and returns an `Option` based on its value:
*
* - If the `Either` is a `Right`, its value is wrapped in a `Some` and
* returned.
* - If the `Either` is a `Left`, the error is discarded, and `None` is
* returned.
*
* This is particularly useful when you only care about the success case
* (`Right`) of an `Either` and want to handle the result using `Option`. By
* using this function, you can convert `Either` into a simpler structure for
* cases where error handling is not required.
*
* @example
* ```ts
* import { Either, Option } from "effect"
*
* console.log(Option.getRight(Either.right("ok")))
* // Output: { _id: 'Option', _tag: 'Some', value: 'ok' }
*
* console.log(Option.getRight(Either.left("err")))
* // Output: { _id: 'Option', _tag: 'None' }
* ```
*
* @see {@link getLeft} for the opposite operation.
*
* @category Conversions
* @since 2.0.0
*/
export declare const getRight: (self: Either) => Option;
/**
* Converts an `Either` into an `Option` by discarding the right value and
* extracting the left value.
*
* **Details**
*
* This function transforms an `Either` into an `Option` as follows:
*
* - If the `Either` is a `Left`, its value is wrapped in a `Some` and returned.
* - If the `Either` is a `Right`, the value is discarded, and `None` is
* returned.
*
* This utility is useful when you only care about the error case (`Left`) of an
* `Either` and want to handle it as an `Option`. By discarding the right value,
* it simplifies error-focused workflows.
*
* @example
* ```ts
* import { Either, Option } from "effect"
*
* console.log(Option.getLeft(Either.right("ok")))
* // Output: { _id: 'Option', _tag: 'None' }
*
* console.log(Option.getLeft(Either.left("err")))
* // Output: { _id: 'Option', _tag: 'Some', value: 'err' }
* ```
*
* @see {@link getRight} for the opposite operation.
*
* @category Conversions
* @since 2.0.0
*/
export declare const getLeft: (self: Either) => Option;
/**
* Returns the value contained in the `Option` if it is `Some`, otherwise
* evaluates and returns the result of `onNone`.
*
* **Details**
*
* This function allows you to provide a fallback value or computation for when
* an `Option` is `None`. If the `Option` contains a value (`Some`), that value
* is returned. If it is empty (`None`), the `onNone` function is executed, and
* its result is returned instead.
*
* This utility is helpful for safely handling `Option` values by ensuring you
* always receive a meaningful result, whether or not the `Option` contains a
* value. It is particularly useful for providing default values or alternative
* logic when working with optional values.
*
* @example
* ```ts
* import { Option } from "effect"
*
* console.log(Option.some(1).pipe(Option.getOrElse(() => 0)))
* // Output: 1
*
* console.log(Option.none().pipe(Option.getOrElse(() => 0)))
* // Output: 0
* ```
*
* @see {@link getOrNull} for a version that returns `null` instead of executing a function.
* @see {@link getOrUndefined} for a version that returns `undefined` instead of executing a function.
*
* @category Getters
* @since 2.0.0
*/
export declare const getOrElse: {
/**
* Returns the value contained in the `Option` if it is `Some`, otherwise
* evaluates and returns the result of `onNone`.
*
* **Details**
*
* This function allows you to provide a fallback value or computation for when
* an `Option` is `None`. If the `Option` contains a value (`Some`), that value
* is returned. If it is empty (`None`), the `onNone` function is executed, and
* its result is returned instead.
*
* This utility is helpful for safely handling `Option` values by ensuring you
* always receive a meaningful result, whether or not the `Option` contains a
* value. It is particularly useful for providing default values or alternative
* logic when working with optional values.
*
* @example
* ```ts
* import { Option } from "effect"
*
* console.log(Option.some(1).pipe(Option.getOrElse(() => 0)))
* // Output: 1
*
* console.log(Option.none().pipe(Option.getOrElse(() => 0)))
* // Output: 0
* ```
*
* @see {@link getOrNull} for a version that returns `null` instead of executing a function.
* @see {@link getOrUndefined} for a version that returns `undefined` instead of executing a function.
*
* @category Getters
* @since 2.0.0
*/
(onNone: LazyArg): (self: Option) => B | A;
/**
* Returns the value contained in the `Option` if it is `Some`, otherwise
* evaluates and returns the result of `onNone`.
*
* **Details**
*
* This function allows you to provide a fallback value or computation for when
* an `Option` is `None`. If the `Option` contains a value (`Some`), that value
* is returned. If it is empty (`None`), the `onNone` function is executed, and
* its result is returned instead.
*
* This utility is helpful for safely handling `Option` values by ensuring you
* always receive a meaningful result, whether or not the `Option` contains a
* value. It is particularly useful for providing default values or alternative
* logic when working with optional values.
*
* @example
* ```ts
* import { Option } from "effect"
*
* console.log(Option.some(1).pipe(Option.getOrElse(() => 0)))
* // Output: 1
*
* console.log(Option.none().pipe(Option.getOrElse(() => 0)))
* // Output: 0
* ```
*
* @see {@link getOrNull} for a version that returns `null` instead of executing a function.
* @see {@link getOrUndefined} for a version that returns `undefined` instead of executing a function.
*
* @category Getters
* @since 2.0.0
*/
(self: Option, onNone: LazyArg): A | B;
};
/**
* Returns the provided `Option` `that` if the current `Option` (`self`) is
* `None`; otherwise, it returns `self`.
*
* **Details**
*
* This function provides a fallback mechanism for `Option` values. If the
* current `Option` is `None` (i.e., it contains no value), the `that` function
* is evaluated, and its resulting `Option` is returned. If the current `Option`
* is `Some` (i.e., it contains a value), the original `Option` is returned
* unchanged.
*
* This is particularly useful for chaining fallback values or computations,
* allowing you to provide alternative `Option` values when the first one is
* empty.
*
* @example
* ```ts
* import { Option } from "effect"
*
* console.log(Option.none().pipe(Option.orElse(() => Option.none())))
* // Output: { _id: 'Option', _tag: 'None' }
*
* console.log(Option.some("a").pipe(Option.orElse(() => Option.none())))
* // Output: { _id: 'Option', _tag: 'Some', value: 'a' }
*
* console.log(Option.none().pipe(Option.orElse(() => Option.some("b"))))
* // Output: { _id: 'Option', _tag: 'Some', value: 'b' }
*
* console.log(Option.some("a").pipe(Option.orElse(() => Option.some("b"))))
* // Output: { _id: 'Option', _tag: 'Some', value: 'a' }
* ```
*
* @category Error handling
* @since 2.0.0
*/
export declare const orElse: {
/**
* Returns the provided `Option` `that` if the current `Option` (`self`) is
* `None`; otherwise, it returns `self`.
*
* **Details**
*
* This function provides a fallback mechanism for `Option` values. If the
* current `Option` is `None` (i.e., it contains no value), the `that` function
* is evaluated, and its resulting `Option` is returned. If the current `Option`
* is `Some` (i.e., it contains a value), the original `Option` is returned
* unchanged.
*
* This is particularly useful for chaining fallback values or computations,
* allowing you to provide alternative `Option` values when the first one is
* empty.
*
* @example
* ```ts
* import { Option } from "effect"
*
* console.log(Option.none().pipe(Option.orElse(() => Option.none())))
* // Output: { _id: 'Option', _tag: 'None' }
*
* console.log(Option.some("a").pipe(Option.orElse(() => Option.none())))
* // Output: { _id: 'Option', _tag: 'Some', value: 'a' }
*
* console.log(Option.none().pipe(Option.orElse(() => Option.some("b"))))
* // Output: { _id: 'Option', _tag: 'Some', value: 'b' }
*
* console.log(Option.some("a").pipe(Option.orElse(() => Option.some("b"))))
* // Output: { _id: 'Option', _tag: 'Some', value: 'a' }
* ```
*
* @category Error handling
* @since 2.0.0
*/
(that: LazyArg