/**
* ```ts
* type Option = None | Some
* ```
*
* `Option` is a container for an optional value of type `A`. If the value of type `A` is present, the `Option` is
* an instance of `Some`, containing the present value of type `A`. If the value is absent, the `Option` is an
* instance of `None`.
*
* An option could be looked at as a collection or foldable structure with either one or zero elements.
* Another way to look at `Option` is: it represents the effect of a possibly failing computation.
*
* @since 2.0.0
*/
import { Alt1 } from './Alt'
import { Alternative1 } from './Alternative'
import { Applicative1 } from './Applicative'
import { Compactable1, Separated } from './Compactable'
import { Either } from './Either'
import { Eq } from './Eq'
import { Extend1 } from './Extend'
import { Filterable1 } from './Filterable'
import { Foldable1 } from './Foldable'
import { Lazy, Predicate, Refinement } from './function'
import { Functor1 } from './Functor'
import { Monad1 } from './Monad'
import { MonadThrow1 } from './MonadThrow'
import { Monoid } from './Monoid'
import { Ord } from './Ord'
import { Semigroup } from './Semigroup'
import { Show } from './Show'
import { PipeableTraverse1, Traversable1 } from './Traversable'
import { PipeableWilt1, PipeableWither1, Witherable1 } from './Witherable'
/**
* @category model
* @since 2.0.0
*/
export interface None {
readonly _tag: 'None'
}
/**
* @category model
* @since 2.0.0
*/
export interface Some {
readonly _tag: 'Some'
readonly value: A
}
/**
* @category model
* @since 2.0.0
*/
export declare type Option = None | Some
/**
* Returns `true` if the option is an instance of `Some`, `false` otherwise.
*
* @example
* import { some, none, isSome } from 'fp-ts/Option'
*
* assert.strictEqual(isSome(some(1)), true)
* assert.strictEqual(isSome(none), false)
*
* @category guards
* @since 2.0.0
*/
export declare const isSome: (fa: Option) => fa is Some
/**
* Returns `true` if the option is `None`, `false` otherwise.
*
* @example
* import { some, none, isNone } from 'fp-ts/Option'
*
* assert.strictEqual(isNone(some(1)), false)
* assert.strictEqual(isNone(none), true)
*
* @category guards
* @since 2.0.0
*/
export declare const isNone: (fa: Option) => fa is None
/**
* `None` doesn't have a constructor, instead you can use it directly as a value. Represents a missing value.
*
* @category constructors
* @since 2.0.0
*/
export declare const none: Option
/**
* Constructs a `Some`. Represents an optional value that exists.
*
* @category constructors
* @since 2.0.0
*/
export declare const some: (a: A) => Option
/**
* Constructs a new `Option` from a nullable type. If the value is `null` or `undefined`, returns `None`, otherwise
* returns the value wrapped in a `Some`.
*
* @example
* import { none, some, fromNullable } from 'fp-ts/Option'
*
* assert.deepStrictEqual(fromNullable(undefined), none)
* assert.deepStrictEqual(fromNullable(null), none)
* assert.deepStrictEqual(fromNullable(1), some(1))
*
* @category constructors
* @since 2.0.0
*/
export declare function fromNullable(a: A): Option>
/**
* Returns a *smart constructor* based on the given predicate.
*
* @example
* import { none, some, fromPredicate } from 'fp-ts/Option'
*
* const getOption = fromPredicate((n: number) => n >= 0)
*
* assert.deepStrictEqual(getOption(-1), none)
* assert.deepStrictEqual(getOption(1), some(1))
*
* @category constructors
* @since 2.0.0
*/
export declare function fromPredicate(refinement: Refinement): (a: A) => Option
export declare function fromPredicate(predicate: Predicate): (a: A) => Option
/**
* Transforms an exception into an `Option`. If `f` throws, returns `None`, otherwise returns the output wrapped in a
* `Some`.
*
* @example
* import { none, some, tryCatch } from 'fp-ts/Option'
*
* assert.deepStrictEqual(
* tryCatch(() => {
* throw new Error()
* }),
* none
* )
* assert.deepStrictEqual(tryCatch(() => 1), some(1))
*
* @category constructors
* @since 2.0.0
*/
export declare function tryCatch(f: Lazy): Option
/**
* Returns the `Left` value of an `Either` if possible.
*
* @example
* import { getLeft, none, some } from 'fp-ts/Option'
* import { right, left } from 'fp-ts/Either'
*
* assert.deepStrictEqual(getLeft(right(1)), none)
* assert.deepStrictEqual(getLeft(left('a')), some('a'))
*
* @category constructors
* @since 2.0.0
*/
export declare function getLeft(ma: Either): Option
/**
* Returns the `Right` value of an `Either` if possible.
*
* @example
* import { getRight, none, some } from 'fp-ts/Option'
* import { right, left } from 'fp-ts/Either'
*
* assert.deepStrictEqual(getRight(right(1)), some(1))
* assert.deepStrictEqual(getRight(left('a')), none)
*
* @category constructors
* @since 2.0.0
*/
export declare function getRight(ma: Either): Option
/**
* Transforms an `Either` to an `Option` discarding the error.
*
* Alias of [getRight](#getRight)
*
* Derivable from `MonadThrow`.
*
* @category constructors
* @since 2.0.0
*/
export declare const fromEither: (ma: Either) => Option
/**
* Takes a (lazy) default value, a function, and an `Option` value, if the `Option` value is `None` the default value is
* returned, otherwise the function is applied to the value inside the `Some` and the result is returned.
*
* @example
* import { some, none, fold } from 'fp-ts/Option'
* import { pipe } from 'fp-ts/function'
*
* assert.strictEqual(
* pipe(
* some(1),
* fold(() => 'a none', a => `a some containing ${a}`)
* ),
* 'a some containing 1'
* )
*
* assert.strictEqual(
* pipe(
* none,
* fold(() => 'a none', a => `a some containing ${a}`)
* ),
* 'a none'
* )
*
* @category destructors
* @since 2.0.0
*/
export declare function fold(onNone: Lazy, onSome: (a: A) => B): (ma: Option) => B
/**
* Extracts the value out of the structure, if it exists. Otherwise returns `null`.
*
* @example
* import { some, none, toNullable } from 'fp-ts/Option'
* import { pipe } from 'fp-ts/function'
*
* assert.strictEqual(
* pipe(
* some(1),
* toNullable
* ),
* 1
* )
* assert.strictEqual(
* pipe(
* none,
* toNullable
* ),
* null
* )
*
* @category destructors
* @since 2.0.0
*/
export declare function toNullable(ma: Option): A | null
/**
* Extracts the value out of the structure, if it exists. Otherwise returns `undefined`.
*
* @example
* import { some, none, toUndefined } from 'fp-ts/Option'
* import { pipe } from 'fp-ts/function'
*
* assert.strictEqual(
* pipe(
* some(1),
* toUndefined
* ),
* 1
* )
* assert.strictEqual(
* pipe(
* none,
* toUndefined
* ),
* undefined
* )
*
* @category destructors
* @since 2.0.0
*/
export declare function toUndefined(ma: Option): A | undefined
/**
* Less strict version of [`getOrElse`](#getOrElse).
*
* @category destructors
* @since 2.6.0
*/
export declare const getOrElseW: (onNone: Lazy) => (ma: Option) => B | A
/**
* Extracts the value out of the structure, if it exists. Otherwise returns the given default value
*
* @example
* import { some, none, getOrElse } from 'fp-ts/Option'
* import { pipe } from 'fp-ts/function'
*
* assert.strictEqual(
* pipe(
* some(1),
* getOrElse(() => 0)
* ),
* 1
* )
* assert.strictEqual(
* pipe(
* none,
* getOrElse(() => 0)
* ),
* 0
* )
*
* @category destructors
* @since 2.0.0
*/
export declare const getOrElse: (onNone: Lazy) => (ma: Option) => A
/**
* Returns a *smart constructor* from a function that returns a nullable value.
*
* @example
* import { fromNullableK, none, some } from 'fp-ts/Option'
*
* const f = (s: string): number | undefined => {
* const n = parseFloat(s)
* return isNaN(n) ? undefined : n
* }
*
* const g = fromNullableK(f)
*
* assert.deepStrictEqual(g('1'), some(1))
* assert.deepStrictEqual(g('a'), none)
*
* @category combinators
* @since 2.9.0
*/
export declare function fromNullableK, B>(
f: (...a: A) => B | null | undefined
): (...a: A) => Option>
/**
* @category combinators
* @since 2.0.0
* @deprecated
*/
export declare const mapNullable: typeof chainNullableK
/**
* This is `chain` + `fromNullable`, useful when working with optional values.
*
* @example
* import { some, none, fromNullable, chainNullableK } from 'fp-ts/Option'
* import { pipe } from 'fp-ts/function'
*
* interface Employee {
* company?: {
* address?: {
* street?: {
* name?: string
* }
* }
* }
* }
*
* const employee1: Employee = { company: { address: { street: { name: 'high street' } } } }
*
* assert.deepStrictEqual(
* pipe(
* fromNullable(employee1.company),
* chainNullableK(company => company.address),
* chainNullableK(address => address.street),
* chainNullableK(street => street.name)
* ),
* some('high street')
* )
*
* const employee2: Employee = { company: { address: { street: {} } } }
*
* assert.deepStrictEqual(
* pipe(
* fromNullable(employee2.company),
* chainNullableK(company => company.address),
* chainNullableK(address => address.street),
* chainNullableK(street => street.name)
* ),
* none
* )
*
* @category combinators
* @since 2.9.0
*/
export declare function chainNullableK(f: (a: A) => B | null | undefined): (ma: Option) => Option
/**
* `map` can be used to turn functions `(a: A) => B` into functions `(fa: F) => F` whose argument and return types
* use the type constructor `F` to represent some computational context.
*
* @category Functor
* @since 2.0.0
*/
export declare const map: (f: (a: A) => B) => (fa: Option) => Option
/**
* Apply a function to an argument under a type constructor.
*
* @category Apply
* @since 2.0.0
*/
export declare const ap: (fa: Option) => (fab: Option<(a: A) => B>) => Option
/**
* Combine two effectful actions, keeping only the result of the first.
*
* Derivable from `Apply`.
*
* @category combinators
* @since 2.0.0
*/
export declare const apFirst: (fb: Option) => (fa: Option) => Option
/**
* Combine two effectful actions, keeping only the result of the second.
*
* Derivable from `Apply`.
*
* @category combinators
* @since 2.0.0
*/
export declare const apSecond: (fb: Option) => (fa: Option) => Option
/**
* Wrap a value into the type constructor.
*
* @category Applicative
* @since 2.7.0
*/
export declare const of: Applicative1['of']
/**
* Composes computations in sequence, using the return value of one computation to determine the next computation.
*
* @category Monad
* @since 2.0.0
*/
export declare const chain: (f: (a: A) => Option) => (ma: Option) => Option
/**
* Composes computations in sequence, using the return value of one computation to determine the next computation and
* keeping only the result of the first.
*
* Derivable from `Monad`.
*
* @category combinators
* @since 2.0.0
*/
export declare const chainFirst: (f: (a: A) => Option) => (ma: Option) => Option
/**
* Derivable from `Monad`.
*
* @category combinators
* @since 2.0.0
*/
export declare const flatten: (mma: Option