Maybe
Maybe:
object
Defined in: Core/Maybe.ts:26
Type Declaration
Section titled “Type Declaration”apply: <
A>(arg) => <B>(maybe) =>Maybe<B>
Applies a function wrapped in a Maybe to a value wrapped in a Maybe.
Type Parameters
Section titled “Type Parameters”A
Parameters
Section titled “Parameters”Maybe<A>
Returns
Section titled “Returns”<B>(maybe) => Maybe<B>
Maybe.map to apply a plain unary function to a Maybe value.
Example
Section titled “Example”assign
Section titled “assign”assign: <
K,A,B>(key,transform) => (maybe) =>Maybe<A&{ [P in string]: B }>
Evaluates a new Maybe using the current accumulator and attaches the output to a new key.
Type Parameters
Section titled “Type Parameters”K extends string
A
B
Parameters
Section titled “Parameters”K
transform
Section titled “transform”(value) => Maybe<B>
Returns
Section titled “Returns”(maybe) => Maybe<A & { [P in string]: B }>
Maybe.assignTo to initialize a pipeline accumulator record.
Example
Section titled “Example”assignTo
Section titled “assignTo”assignTo: <
K>(key) => <A>(maybe) =>Maybe<{ [P in string]: A }>
Converts a Maybe value into an object containing a single property. Initiates the pipeline accumulator record.
Type Parameters
Section titled “Type Parameters”K extends string
Parameters
Section titled “Parameters”K
Returns
Section titled “Returns”<A>(maybe) => Maybe<{ [P in string]: A }>
Maybe.assign to append additional fields to an accumulator record.
Example
Section titled “Example”chain: <
A,B>(transform) => (maybe) =>Maybe<B>
Chains Maybe computations. If the first is Some, passes the value to transform.
If the first is None, propagates None.
Type Parameters
Section titled “Type Parameters”A
B
Parameters
Section titled “Parameters”transform
Section titled “transform”(value) => Maybe<B>
Returns
Section titled “Returns”(maybe) => Maybe<B>
Maybe.map for transforming with plain non-optional functions.
Example
Section titled “Example”filter
Section titled “filter”filter: {<
A,B>(refinement): (maybe) =>Maybe<B>; <A>(predicate): (maybe) =>Maybe<A>; }
Filters a Maybe based on a predicate or type guard. Returns None if the predicate returns false or if the Maybe is already None.
Call Signature
Section titled “Call Signature”<
A,B>(refinement): (maybe) =>Maybe<B>
Type Parameters
Section titled “Type Parameters”A
B
Parameters
Section titled “Parameters”refinement
Section titled “refinement”(value) => value is B
Returns
Section titled “Returns”(maybe) => Maybe<B>
Call Signature
Section titled “Call Signature”<
A>(predicate): (maybe) =>Maybe<A>
Type Parameters
Section titled “Type Parameters”A
Parameters
Section titled “Parameters”predicate
Section titled “predicate”(value) => boolean
Returns
Section titled “Returns”(maybe) => Maybe<A>
- Maybe.from.Predicate to lift a raw value into a Maybe based on a predicate.
- Maybe.map to transform the value without filtering.
Example
Section titled “Example”from:
object
from.nullable
Section titled “from.nullable”nullable: <
A>(value) =>Maybe<NonNullable<A>>
Creates a Maybe from a nullable value. Returns None if the value is null or undefined, Some otherwise.
Type Parameters
Section titled “Type Parameters”A
Parameters
Section titled “Parameters”A
Returns
Section titled “Returns”Maybe<NonNullable<A>>
Maybe.to.nullable to extract a value back into a nullable primitive.
Example
Section titled “Example”from.Predicate
Section titled “from.Predicate”Predicate: {<
A,B>(refinement): (value) =>Maybe<B>; <A>(predicate): (value) =>Maybe<A>; }
Creates a Maybe from a predicate or type guard applied to a value. Returns Some if the predicate passes, None otherwise.
Call Signature
Section titled “Call Signature”<
A,B>(refinement): (value) =>Maybe<B>
Type Parameters
Section titled “Type Parameters”A
B
Parameters
Section titled “Parameters”refinement
Section titled “refinement”(value) => value is B
Returns
Section titled “Returns”(value) => Maybe<B>
Call Signature
Section titled “Call Signature”<
A>(predicate): (value) =>Maybe<A>
Type Parameters
Section titled “Type Parameters”A
Parameters
Section titled “Parameters”predicate
Section titled “predicate”(value) => boolean
Returns
Section titled “Returns”(value) => Maybe<A>
Maybe.filter to filter an existing Maybe with a predicate or type guard.
Example
Section titled “Example”from.Result
Section titled “from.Result”Result: <
E,A>(result) =>Maybe<A>
Creates a Maybe from a Result. Ok becomes Some, Err becomes None (the error is discarded).
Type Parameters
Section titled “Type Parameters”E
A
Parameters
Section titled “Parameters”result
Section titled “result”Result<E, A>
Returns
Section titled “Returns”Maybe<A>
Maybe.to.Result to convert a Maybe to a Result with an explicit error.
Example
Section titled “Example”from.Validation
Section titled “from.Validation”Validation: <
E,A>(validation) =>Maybe<A>
Creates a Maybe from a Validation. Passed becomes Some, Failed becomes None (the errors are discarded).
Type Parameters
Section titled “Type Parameters”E
A
Parameters
Section titled “Parameters”validation
Section titled “validation”Validation<E, A>
Returns
Section titled “Returns”Maybe<A>
Maybe.to.Validation to convert a Maybe to a Validation with an explicit error.
Example
Section titled “Example”getOrElse
Section titled “getOrElse”getOrElse: <
B>(fallback) => <A>(maybe) =>B|A
Returns the value inside a Maybe, or a default value if None.
The fallback is a thunk () => B — evaluated only when the Maybe is None.
The fallback can be a different type, widening the result to A | B.
Type Parameters
Section titled “Type Parameters”B
Parameters
Section titled “Parameters”fallback
Section titled “fallback”() => B
Returns
Section titled “Returns”<A>(maybe) => B | A
- Maybe.match to handle both the Some and None cases.
- Maybe.to.nullable to extract the value as a nullable primitive.
Example
Section titled “Example”is:
object
is.none
Section titled “is.none”none: <
A>(maybe) =>maybe is None=isNone
Type guard that checks if a Maybe is None.
Type Parameters
Section titled “Type Parameters”A
Parameters
Section titled “Parameters”Maybe<A>
Returns
Section titled “Returns”maybe is None
Maybe.is.some to check if a Maybe is a Some variant containing a value.
Example
Section titled “Example”is.some
Section titled “is.some”some: <
A>(maybe) =>maybe is Some<A>=isSome
Type guard that checks if a Maybe is Some.
Type Parameters
Section titled “Type Parameters”A
Parameters
Section titled “Parameters”Maybe<A>
Returns
Section titled “Returns”maybe is Some<A>
Maybe.is.none to check if a Maybe is a None (empty) variant.
Example
Section titled “Example”make:
object
make.none
Section titled “make.none”none: () =>
None=makeNone
Creates a None (empty Maybe).
Returns
Section titled “Returns”Example
Section titled “Example”make.some
Section titled “make.some”some: <
A>(value) =>Some<A> =makeSome
Creates a Some containing the given value.
Type Parameters
Section titled “Type Parameters”A
Parameters
Section titled “Parameters”A
Returns
Section titled “Returns”Some<A>
Example
Section titled “Example”map: <
A,B>(transform) => (maybe) =>Maybe<B>
Transforms the value inside a Maybe if it exists.
Type Parameters
Section titled “Type Parameters”A
B
Parameters
Section titled “Parameters”transform
Section titled “transform”(value) => B
Returns
Section titled “Returns”(maybe) => Maybe<B>
Maybe.chain for functions that return a Maybe.
Example
Section titled “Example”match: <
A,B>(cases) => (maybe) =>B
Pattern matches on a Maybe, returning the result of the matching case.
Type Parameters
Section titled “Type Parameters”A
B
Parameters
Section titled “Parameters”() => B
(value) => B
Returns
Section titled “Returns”(maybe) => B
Maybe.getOrElse to extract the value with a fallback.
Example
Section titled “Example”recover
Section titled “recover”recover: <
B>(fallback) => <A>(maybe) =>Maybe<B|A>
Recovers from a None by providing a fallback Maybe.
The fallback can produce a different type, widening the result to Maybe<A | B>.
Type Parameters
Section titled “Type Parameters”B
Parameters
Section titled “Parameters”fallback
Section titled “fallback”() => Maybe<B>
Returns
Section titled “Returns”<A>(maybe) => Maybe<B | A>
Maybe.getOrElse to unwrap a Maybe with a fallback value instead of another Maybe.
Example
Section titled “Example”sequence
Section titled “sequence”sequence:
object
sequence.Result
Section titled “sequence.Result”Swaps the outer Maybe and inner Result context.
Some(Ok(a)) becomes Ok(Some(a)), Some(Err(e)) becomes Err(e), and None becomes Ok(None).
Type Parameters
Section titled “Type Parameters”E
A
Parameters
Section titled “Parameters”Returns
Section titled “Returns”Result.sequence.Maybe to swap outer Result and inner Maybe contexts.
Example
Section titled “Example”struct
Section titled “struct”struct: <
T>(fields) =>Maybe<{ [K in string | number | symbol]: T[K] extends Maybe<A> ? A : never }>
Combines a record of Maybes into a single Maybe of a record. Evaluates fields in key order and short-circuits on the first None.
Type Parameters
Section titled “Type Parameters”T extends Record<string, Maybe<unknown>>
Parameters
Section titled “Parameters”fields
Section titled “fields”T
Returns
Section titled “Returns”Maybe<{ [K in string | number | symbol]: T[K] extends Maybe<A> ? A : never }>
Maybe.assign to combine values sequentially with inter-field dependencies.
Example
Section titled “Example”tap: <
A>(sideEffect) => (maybe) =>Maybe<A>
Executes a side effect on the value without changing the Maybe. Useful for logging or debugging.
Type Parameters
Section titled “Type Parameters”A
Parameters
Section titled “Parameters”sideEffect
Section titled “sideEffect”(value) => void
Returns
Section titled “Returns”(maybe) => Maybe<A>
Maybe.tapNone for running side effects on None.
Example
Section titled “Example”tapNone
Section titled “tapNone”tapNone: (
sideEffect) => <A>(maybe) =>Maybe<A>
Executes a side effect when the Maybe is None, without changing the Maybe.
Parameters
Section titled “Parameters”sideEffect
Section titled “sideEffect”() => void
Returns
Section titled “Returns”<A>(maybe) => Maybe<A>
Maybe.tap for running side effects on Some.
Example
Section titled “Example”to:
object
to.nullable
Section titled “to.nullable”nullable: <
A>(maybe) =>A|null
Extracts the value from a Maybe, returning null if None.
Type Parameters
Section titled “Type Parameters”A
Parameters
Section titled “Parameters”Maybe<A>
Returns
Section titled “Returns”A | null
- Maybe.to.undefined to extract value returning undefined on None.
- Maybe.from.nullable to create a Maybe from a nullable value.
Example
Section titled “Example”to.Result
Section titled “to.Result”Result: <
E>(onNone) => <A>(maybe) =>Result<E,A>
Converts a Maybe to a Result. Some becomes Ok, None becomes Err with the provided error.
Type Parameters
Section titled “Type Parameters”E
Parameters
Section titled “Parameters”onNone
Section titled “onNone”() => E
Returns
Section titled “Returns”<A>(maybe) => Result<E, A>
- Maybe.from.Result to convert a Result back into a Maybe.
- Maybe.to.Validation to convert a Maybe into an error-accumulating Validation.
Example
Section titled “Example”to.undefined
Section titled “to.undefined”undefined: <
A>(maybe) =>A|undefined
Extracts the value from a Maybe, returning undefined if None.
Type Parameters
Section titled “Type Parameters”A
Parameters
Section titled “Parameters”Maybe<A>
Returns
Section titled “Returns”A | undefined
Maybe.to.nullable to extract value returning null on None.
Example
Section titled “Example”to.Validation
Section titled “to.Validation”Validation: <
E>(onNone) => <A>(maybe) =>Validation<E,A>
Converts a Maybe to a Validation.
Some becomes Passed, None becomes Failed with error produced by onNone.
Type Parameters
Section titled “Type Parameters”E
Parameters
Section titled “Parameters”onNone
Section titled “onNone”() => E
Returns
Section titled “Returns”<A>(maybe) => Validation<E, A>
- Maybe.to.Result to convert a Maybe into a fail-fast Result.
- Maybe.from.Validation to convert a Validation back into a Maybe.
Example
Section titled “Example”tryCatch
Section titled “tryCatch”tryCatch: <
A>(operation) =>Maybe<A>
Wraps a synchronous operation that may throw, returning a Maybe<A>.
Returns Some(value) if successful, or None if an exception is thrown.
Type Parameters
Section titled “Type Parameters”A
Parameters
Section titled “Parameters”operation
Section titled “operation”() => A
Returns
Section titled “Returns”Maybe<A>