Option
Instances
- Alt
- Alternative
- Applicative
- Apply
- Chain
- ChainRec
- Compactable
- Extend
- Filterable
- Foldable
- FromEither
- Functor
- Monad
- MonadThrow
- Pointed
- Traversable
- Witherable
- Zero
- getEq
- getMonoid
- getOrd
- getShow
Combinators
Constructors
Conversions
Do notation
Error handling
Filtering
Folding
Instance methods
Interop
Legacy
Lifting
Mapping
Model
Pattern matching
Refinements
Sequencing
Traversing
- sequence
- sequenceArray
- traverse
- traverseArray
- traverseArrayWithIndex
- traverseReadonlyArrayWithIndex
- traverseReadonlyNonEmptyArrayWithIndex
Type lambdas
Utilities
- ApT
- ap
- apFirst
- apSecond
- do
- duplicate
- elem
- exists
- extend
getApplyMonoid(deprecated)getApplySemigroup(deprecated)getFirstMonoid(deprecated)getLastMonoid(deprecated)getRefinement(deprecated)- let
mapNullable(deprecated)option(deprecated)- throwError
- zero
Instances
Alt
Signature
export declare const Alt: Alt1<URI>
Details
- Added in 0.1.0
License
- MIT – Copyright (c) 2017-present Giulio Canti
Alternative
Signature
export declare const Alternative: Alternative1<URI>
Details
- Added in 0.1.0
License
- MIT – Copyright (c) 2017-present Giulio Canti
Applicative
Signature
export declare const Applicative: Applicative1<URI>
Details
- Added in 0.1.0
License
- MIT – Copyright (c) 2017-present Giulio Canti
Apply
Signature
export declare const Apply: Apply1<URI>
Details
- Added in 0.1.0
License
- MIT – Copyright (c) 2017-present Giulio Canti
Chain
Signature
export declare const Chain: chainable.Chain1<URI>
Details
- Added in 0.1.0
License
- MIT – Copyright (c) 2017-present Giulio Canti
ChainRec
ChainRec for Option
Signature
export declare const ChainRec: ChainRec1<URI>
Details
- Added in 0.1.0
License
- MIT – Copyright (c) 2022-present Jacob Alford
Compactable
Signature
export declare const Compactable: Compactable1<URI>
Details
- Added in 0.1.0
License
- MIT – Copyright (c) 2017-present Giulio Canti
Extend
Signature
export declare const Extend: Extend1<URI>
Details
- Added in 0.1.0
License
- MIT – Copyright (c) 2017-present Giulio Canti
Filterable
Signature
export declare const Filterable: Filterable1<URI>
Details
- Added in 0.1.0
License
- MIT – Copyright (c) 2017-present Giulio Canti
Foldable
Signature
export declare const Foldable: Foldable1<URI>
Details
- Added in 0.1.0
License
- MIT – Copyright (c) 2017-present Giulio Canti
FromEither
Signature
export declare const FromEither: FromEither1<URI>
Details
- Added in 0.1.0
License
- MIT – Copyright (c) 2017-present Giulio Canti
Functor
Signature
export declare const Functor: Functor1<URI>
Details
- Added in 0.1.0
License
- MIT – Copyright (c) 2017-present Giulio Canti
Monad
Signature
export declare const Monad: Monad1<URI>
Details
- Added in 0.1.0
License
- MIT – Copyright (c) 2017-present Giulio Canti
MonadThrow
Signature
export declare const MonadThrow: MonadThrow1<URI>
Details
- Added in 0.1.0
License
- MIT – Copyright (c) 2017-present Giulio Canti
Pointed
Signature
export declare const Pointed: Pointed1<URI>
Details
- Added in 0.1.0
License
- MIT – Copyright (c) 2017-present Giulio Canti
Traversable
Signature
export declare const Traversable: Traversable1<URI>
Details
- Added in 0.1.0
License
- MIT – Copyright (c) 2017-present Giulio Canti
Witherable
Signature
export declare const Witherable: Witherable1<URI>
Details
- Added in 0.1.0
License
- MIT – Copyright (c) 2017-present Giulio Canti
Zero
Signature
export declare const Zero: Zero1<URI>
Details
- Added in 0.1.0
License
- MIT – Copyright (c) 2017-present Giulio Canti
getEq
Signature
export declare const getEq: <A>(E: Eq<A>) => Eq<Option<A>>
Details
- Added in 0.1.0
Example
import { none, some, getEq } from '@fp-tx/core/Option'
import * as N from '@fp-tx/core/number'
const E = getEq(N.Eq)
assert.strictEqual(E.equals(none, none), true)
assert.strictEqual(E.equals(none, some(1)), false)
assert.strictEqual(E.equals(some(1), none), false)
assert.strictEqual(E.equals(some(1), some(2)), false)
assert.strictEqual(E.equals(some(1), some(1)), true)
License
- MIT – Copyright (c) 2017-present Giulio Canti
getMonoid
Monoid returning the left-most non-None
value. If both operands are Some
s then the inner values are concatenated using the provided Semigroup
| x | y | concat(x, y) | | ------- | ------- | ------------------ | | none | none | none | | some(a) | none | some(a) | | none | some(b) | some(b) | | some(a) | some(b) | some(concat(a, b)) |
Signature
export declare const getMonoid: <A>(S: Semigroup<A>) => Monoid<Option<A>>
Details
- Added in 0.1.0
Example
import { getMonoid, some, none } from '@fp-tx/core/Option'
import { SemigroupSum } from '@fp-tx/core/number'
const M = getMonoid(SemigroupSum)
assert.deepStrictEqual(M.concat(none, none), none)
assert.deepStrictEqual(M.concat(some(1), none), some(1))
assert.deepStrictEqual(M.concat(none, some(1)), some(1))
assert.deepStrictEqual(M.concat(some(1), some(2)), some(3))
License
- MIT – Copyright (c) 2017-present Giulio Canti
getOrd
The Ord
instance allows Option
values to be compared with compare
, whenever there is an Ord
instance for the type the Option
contains.
None
is considered to be less than any Some
value.
Signature
export declare const getOrd: <A>(O: Ord<A>) => Ord<Option<A>>
Details
- Added in 0.1.0
Example
import { none, some, getOrd } from '@fp-tx/core/Option'
import * as N from '@fp-tx/core/number'
const O = getOrd(N.Ord)
assert.strictEqual(O.compare(none, none), 0)
assert.strictEqual(O.compare(none, some(1)), -1)
assert.strictEqual(O.compare(some(1), none), 1)
assert.strictEqual(O.compare(some(1), some(2)), -1)
assert.strictEqual(O.compare(some(1), some(1)), 0)
License
- MIT – Copyright (c) 2017-present Giulio Canti
getShow
Signature
export declare const getShow: <A>(S: Show<A>) => Show<Option<A>>
Details
- Added in 0.1.0
License
- MIT – Copyright (c) 2017-present Giulio Canti
Combinators
tap
Composes computations in sequence, using the return value of one computation to determine the next computation and keeping only the result of the first.
Signature
export declare const tap: {
<A, _>(self: Option<A>, f: (a: A) => Option<_>): Option<A>
<A, _>(f: (a: A) => Option<_>): (self: Option<A>) => Option<A>
}
Details
- Added in 0.1.0
License
- MIT – Copyright (c) 2017-present Giulio Canti
tapEither
Composes computations in sequence, using the return value of one computation to determine the next computation and keeping only the result of the first.
Signature
export declare const tapEither: {
<A, E, _>(f: (a: A) => Either<E, _>): (self: Option<A>) => Option<A>
<A, E, _>(self: Option<A>, f: (a: A) => Either<E, _>): Option<A>
}
Details
- Added in 0.1.0
Example
import { pipe } from '@fp-tx/core/function'
import * as O from '@fp-tx/core/Option'
import * as E from '@fp-tx/core/Either'
const compute = (value: number) =>
pipe(
O.of(value),
O.tapEither(value => (value > 0 ? E.right('ok') : E.left('error'))),
)
assert.deepStrictEqual(compute(1), O.of(1))
assert.deepStrictEqual(compute(-42), O.none)
License
- MIT – Copyright (c) 2017-present Giulio Canti
Constructors
getLeft
Returns the Left
value of an Either
if possible.
Signature
export declare const getLeft: <E, A>(ma: Either<E, A>) => Option<E>
Details
- Added in 0.1.0
Example
import { getLeft, none, some } from '@fp-tx/core/Option'
import { right, left } from '@fp-tx/core/Either'
assert.deepStrictEqual(getLeft(right(1)), none)
assert.deepStrictEqual(getLeft(left('a')), some('a'))
License
- MIT – Copyright (c) 2017-present Giulio Canti
getRight
Returns the Right
value of an Either
if possible.
Signature
export declare const getRight: <E, A>(ma: Either<E, A>) => Option<A>
Details
- Added in 0.1.0
Example
import { getRight, none, some } from '@fp-tx/core/Option'
import { right, left } from '@fp-tx/core/Either'
assert.deepStrictEqual(getRight(right(1)), some(1))
assert.deepStrictEqual(getRight(left('a')), none)
License
- MIT – Copyright (c) 2017-present Giulio Canti
none
None
doesn't have a constructor, instead you can use it directly as a value. Represents a missing value.
Signature
export declare const none: Option<never>
Details
- Added in 0.1.0
License
- MIT – Copyright (c) 2017-present Giulio Canti
of
Signature
export declare const of: <A>(a: A) => Option<A>
Details
- Added in 0.1.0
License
- MIT – Copyright (c) 2017-present Giulio Canti
some
Constructs a Some
. Represents an optional value that exists.
Signature
export declare const some: <A>(a: A) => Option<A>
Details
- Added in 0.1.0
License
- MIT – Copyright (c) 2017-present Giulio Canti
Conversions
fromEither
Transforms an Either
to an Option
discarding the error.
Alias of getRight
Signature
export declare const fromEither: <A>(fa: Either<unknown, A>) => Option<A>
Details
- Added in 0.1.0
License
- MIT – Copyright (c) 2017-present Giulio Canti
fromNullable
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
.
Signature
export declare const fromNullable: <A>(a: A) => Option<NonNullable<A>>
Details
- Added in 0.1.0
Example
import { none, some, fromNullable } from '@fp-tx/core/Option'
assert.deepStrictEqual(fromNullable(undefined), none)
assert.deepStrictEqual(fromNullable(null), none)
assert.deepStrictEqual(fromNullable(1), some(1))
License
- MIT – Copyright (c) 2017-present Giulio Canti
toNullable
Extracts the value out of the structure, if it exists. Otherwise returns null
.
Signature
export declare const toNullable: <A>(ma: Option<A>) => A | null
Details
- Added in 0.1.0
Example
import { some, none, toNullable } from '@fp-tx/core/Option'
import { pipe } from '@fp-tx/core/function'
assert.strictEqual(pipe(some(1), toNullable), 1)
assert.strictEqual(pipe(none, toNullable), null)
License
- MIT – Copyright (c) 2017-present Giulio Canti
toUndefined
Extracts the value out of the structure, if it exists. Otherwise returns undefined
.
Signature
export declare const toUndefined: <A>(ma: Option<A>) => A | undefined
Details
- Added in 0.1.0
Example
import { some, none, toUndefined } from '@fp-tx/core/Option'
import { pipe } from '@fp-tx/core/function'
assert.strictEqual(pipe(some(1), toUndefined), 1)
assert.strictEqual(pipe(none, toUndefined), undefined)
License
- MIT – Copyright (c) 2017-present Giulio Canti
Do notation
Do
Signature
export declare const Do: Option<{}>
Details
- Added in 0.1.0
License
- MIT – Copyright (c) 2017-present Giulio Canti
apS
Signature
export declare const apS: <N extends string, A, B>(
name: Exclude<N, keyof A>,
fb: Option<B>,
) => (fa: Option<A>) => Option<{ readonly [K in N | keyof A]: K extends keyof A ? A[K] : B }>
Details
- Added in 0.1.0
License
- MIT – Copyright (c) 2017-present Giulio Canti
bind
Signature
export declare const bind: <N extends string, A, B>(
name: Exclude<N, keyof A>,
f: (a: A) => Option<B>,
) => (ma: Option<A>) => Option<{ readonly [K in N | keyof A]: K extends keyof A ? A[K] : B }>
Details
- Added in 0.1.0
License
- MIT – Copyright (c) 2017-present Giulio Canti
bindTo
Signature
export declare const bindTo: <N extends string>(name: N) => <A>(fa: Option<A>) => Option<{ readonly [K in N]: A }>
Details
- Added in 0.1.0
License
- MIT – Copyright (c) 2017-present Giulio Canti
guard
Signature
export declare const guard: (b: boolean) => Option<void>
Details
- Added in 0.1.0
License
- MIT – Copyright (c) 2017-present Giulio Canti
Error handling
getOrElse
Extracts the value out of the structure, if it exists. Otherwise returns the given default value
Signature
export declare const getOrElse: <A>(onNone: LazyArg<A>) => (ma: Option<A>) => A
Details
- Added in 0.1.0
Example
import { some, none, getOrElse } from '@fp-tx/core/Option'
import { pipe } from '@fp-tx/core/function'
assert.strictEqual(
pipe(
some(1),
getOrElse(() => 0),
),
1,
)
assert.strictEqual(
pipe(
none,
getOrElse(() => 0),
),
0,
)
License
- MIT – Copyright (c) 2017-present Giulio Canti
getOrElseW
Less strict version of getOrElse
.
The W
suffix (short for Widening) means that the handler return type will be merged.
Signature
export declare const getOrElseW: <B>(onNone: LazyArg<B>) => <A>(ma: Option<A>) => B | A
Details
- Added in 0.1.0
License
- MIT – Copyright (c) 2017-present Giulio Canti
orElse
Returns the provided Option
that
if self
is None
, otherwise returns self
.
Signature
export declare const orElse: {
<B>(that: LazyArg<Option<B>>): <A>(self: Option<A>) => Option<A | B>
<A, B>(self: Option<A>, that: LazyArg<Option<B>>): Option<A | B>
}
Details
- Added in 0.1.0
License
- MIT – Copyright (c) 2017-present Giulio Canti
Filtering
compact
Signature
export declare const compact: <A>(fa: Option<Option<A>>) => Option<A>
Details
- Added in 0.1.0
License
- MIT – Copyright (c) 2017-present Giulio Canti
filter
Signature
export declare const filter: {
<A, B extends A>(refinement: Refinement<A, B>): (fa: Option<A>) => Option<B>
<A>(predicate: Predicate<A>): <B extends A>(fb: Option<B>) => Option<B>
<A>(predicate: Predicate<A>): (fa: Option<A>) => Option<A>
}
Details
- Added in 0.1.0
License
- MIT – Copyright (c) 2017-present Giulio Canti
filterMap
Signature
export declare const filterMap: <A, B>(f: (a: A) => Option<B>) => (fa: Option<A>) => Option<B>
Details
- Added in 0.1.0
License
- MIT – Copyright (c) 2017-present Giulio Canti
partition
Signature
export declare const partition: {
<A, B extends A>(refinement: Refinement<A, B>): (fa: Option<A>) => Separated<Option<A>, Option<B>>
<A>(predicate: Predicate<A>): <B extends A>(fb: Option<B>) => Separated<Option<B>, Option<B>>
<A>(predicate: Predicate<A>): (fa: Option<A>) => Separated<Option<A>, Option<A>>
}
Details
- Added in 0.1.0
License
- MIT – Copyright (c) 2017-present Giulio Canti
partitionMap
Signature
export declare const partitionMap: <A, B, C>(
f: (a: A) => Either<B, C>,
) => (fa: Option<A>) => Separated<Option<B>, Option<C>>
Details
- Added in 0.1.0
License
- MIT – Copyright (c) 2017-present Giulio Canti
separate
Signature
export declare const separate: <A, B>(ma: Option<Either<A, B>>) => Separated<Option<A>, Option<B>>
Details
- Added in 0.1.0
License
- MIT – Copyright (c) 2017-present Giulio Canti
wilt
Signature
export declare const wilt: PipeableWilt1<URI>
Details
- Added in 0.1.0
License
- MIT – Copyright (c) 2017-present Giulio Canti
wither
Signature
export declare const wither: PipeableWither1<URI>
Details
- Added in 0.1.0
License
- MIT – Copyright (c) 2017-present Giulio Canti
Folding
foldMap
Signature
export declare const foldMap: <M>(M: Monoid<M>) => <A>(f: (a: A) => M) => (fa: Option<A>) => M
Details
- Added in 0.1.0
License
- MIT – Copyright (c) 2017-present Giulio Canti
reduce
Signature
export declare const reduce: <A, B>(b: B, f: (b: B, a: A) => B) => (fa: Option<A>) => B
Details
- Added in 0.1.0
License
- MIT – Copyright (c) 2017-present Giulio Canti
reduceRight
Signature
export declare const reduceRight: <A, B>(b: B, f: (a: A, b: B) => B) => (fa: Option<A>) => B
Details
- Added in 0.1.0
License
- MIT – Copyright (c) 2017-present Giulio Canti
Instance methods
chainRec
Signature
export declare const chainRec: ChainRec1<URI>['chainRec']
Details
- Added in 0.1.0
License
- MIT – Copyright (c) 2022-present Jacob Alford
Interop
tryCatch
Transforms an exception into an Option
. If f
throws, returns None
, otherwise returns the output wrapped in a Some
.
See also tryCatchK
.
Signature
export declare const tryCatch: <A>(f: LazyArg<A>) => Option<A>
Details
- Added in 0.1.0
Example
import { none, some, tryCatch } from '@fp-tx/core/Option'
assert.deepStrictEqual(
tryCatch(() => {
throw new Error()
}),
none,
)
assert.deepStrictEqual(
tryCatch(() => 1),
some(1),
)
License
- MIT – Copyright (c) 2017-present Giulio Canti
tryCatchK
Converts a function that may throw to one returning a Option
.
Signature
export declare const tryCatchK: <A extends readonly unknown[], B>(f: (...a: A) => B) => (...a: A) => Option<B>
Details
- Added in 0.1.0
License
- MIT – Copyright (c) 2017-present Giulio Canti
Legacy
alt
Alias of orElse
.
Signature
export declare const alt: <A>(that: LazyArg<Option<A>>) => (fa: Option<A>) => Option<A>
Details
- Added in 0.1.0
License
- MIT – Copyright (c) 2017-present Giulio Canti
altW
Alias of orElse
.
Less strict version of alt
.
The W
suffix (short for Widening) means that the return types will be merged.
Signature
export declare const altW: <B>(that: LazyArg<Option<B>>) => <A>(fa: Option<A>) => Option<A | B>
Details
- Added in 0.1.0