Result
Result:
object
Defined in: Core/Result.ts:26
Type Declaration
Section titled “Type Declaration”ap: <
E,A>(arg) => <B>(data) =>Result<E,B>
Applies a function wrapped in a Result to a value wrapped in a Result.
Type Parameters
Section titled “Type Parameters”E
A
Parameters
Section titled “Parameters”Result<E, A>
Returns
Section titled “Returns”<B>(data) => Result<E, B>
Example
Section titled “Example”bimap: <
E1,E2,A,B>(onErr,onOk) => (data) =>Result<E2,B>
Transforms both branches of a Result simultaneously.
Applies onErr to Err values and onOk to Ok values.
Type Parameters
Section titled “Type Parameters”E1
E2
A
B
Parameters
Section titled “Parameters”(e) => E2
(a) => B
Returns
Section titled “Returns”(data) => Result<E2, B>
Example
Section titled “Example”bind: <
K,E,A,B>(key,f) => (data) =>Result<E,A&{ [P in string]: B }>
Evaluates a new Result using the current accumulator and attaches the output to a new key.
Type Parameters
Section titled “Type Parameters”K extends string
E
A
B
Parameters
Section titled “Parameters”K
(a) => Result<E, B>
Returns
Section titled “Returns”(data) => Result<E, A & { [P in string]: B }>
Example
Section titled “Example”bindTo
Section titled “bindTo”bindTo: <
K>(key) => <E,A>(data) =>Result<E,{ [P in string]: A }>
Converts a Result 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”<E, A>(data) => Result<E, { [P in string]: A }>
Example
Section titled “Example”chain: <
E2,A,B>(f) => <E1>(data) =>Result<E2|E1,B>
Chains Result computations. If the first is Ok, passes the value to f. If the first is Err, propagates the error.
Type Parameters
Section titled “Type Parameters”E2
A
B
Parameters
Section titled “Parameters”(a) => Result<E2, B>
Returns
Section titled “Returns”<E1>(data) => Result<E2 | E1, B>
Example
Section titled “Example”ensure
Section titled “ensure”ensure: <
A,E2>(predicate,onFail) => <E1>(data) =>Result<E2|E1,A>
Narrows an Ok value with a predicate, converting to Err(onFail(a)) if the predicate returns false.
Type Parameters
Section titled “Type Parameters”A
E2
Parameters
Section titled “Parameters”predicate
Section titled “predicate”(a) => boolean
onFail
Section titled “onFail”(a) => E2
Returns
Section titled “Returns”<E1>(data) => Result<E2 | E1, A>
Example
Section titled “Example”fold: <
E,A,B>(onErr,onOk) => (data) =>B
Extracts the value from a Result by providing handlers for both cases.
Type Parameters
Section titled “Type Parameters”E
A
B
Parameters
Section titled “Parameters”(e) => B
(a) => B
Returns
Section titled “Returns”(data) => B
Example
Section titled “Example”from:
object
from.Maybe
Section titled “from.Maybe”Maybe: <
E>(onNone) => <A>(maybe) =>Result<E,A>
Creates a Result from a Maybe. Some becomes Ok, None becomes error from onNone.
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>
Example
Section titled “Example”from.nullable
Section titled “from.nullable”nullable: <
E>(onNull) => <A>(value) =>Result<E,A>
Creates a Result from a nullable value. Returns Ok if the value is not null or undefined, error from onNull otherwise.
Type Parameters
Section titled “Type Parameters”E
Parameters
Section titled “Parameters”onNull
Section titled “onNull”() => E
Returns
Section titled “Returns”<A>(value) => Result<E, A>
Example
Section titled “Example”from.Predicate
Section titled “from.Predicate”Predicate: <
E,A>(pred,onFalse) => (a) =>Result<E,A>
Creates a Result from a predicate applied to a value. Returns Ok if the predicate passes, Err from onFalse otherwise.
Type Parameters
Section titled “Type Parameters”E
A
Parameters
Section titled “Parameters”(a) => boolean
onFalse
Section titled “onFalse”(a) => E
Returns
Section titled “Returns”(a) => Result<E, A>
Example
Section titled “Example”from.Validation
Section titled “from.Validation”Validation: <
E1,E2,A>(combineErrors) => (val) =>Result<E2,A>
Converts a Validation to a Result, combining accumulated errors using combineErrors.
Passed(a) becomes Ok(a); Failed(errors) becomes Err(combineErrors(errors)).
Type Parameters
Section titled “Type Parameters”E1
E2
A
Parameters
Section titled “Parameters”combineErrors
Section titled “combineErrors”(errors) => E2
Returns
Section titled “Returns”(val) => Result<E2, A>
Example
Section titled “Example”getOrElse
Section titled “getOrElse”getOrElse: <
B>(defaultValue) => <E,A>(data) =>B|A
Returns the success value or a default value if the Result is an error.
The default is a thunk () => B — evaluated only when the Result is Err.
The default can be a different type, widening the result to A | B.
Type Parameters
Section titled “Type Parameters”B
Parameters
Section titled “Parameters”defaultValue
Section titled “defaultValue”() => B
Returns
Section titled “Returns”<E, A>(data) => B | A
Example
Section titled “Example”is:
object
is.err
Section titled “is.err”err: <
E,A>(data) =>data is Err<E>=isErr
Type guard that checks if a Result is Err.
Type Parameters
Section titled “Type Parameters”E
A
Parameters
Section titled “Parameters”Result<E, A>
Returns
Section titled “Returns”data is Err<E>
Example
Section titled “Example”ok: <
E,A>(data) =>data is Ok<A>=isOk
Type guard that checks if a Result is Ok.
Type Parameters
Section titled “Type Parameters”E
A
Parameters
Section titled “Parameters”Result<E, A>
Returns
Section titled “Returns”data is Ok<A>
Example
Section titled “Example”make:
object
make.err
Section titled “make.err”err: <
E>(e) =>Err<E> =makeErr
Creates a failed Result with the given error.
Type Parameters
Section titled “Type Parameters”E
Parameters
Section titled “Parameters”E
Returns
Section titled “Returns”Err<E>
Example
Section titled “Example”make.ok
Section titled “make.ok”ok: <
A>(value) =>Ok<A> =makeOk
Creates a successful Result with the given value.
Type Parameters
Section titled “Type Parameters”A
Parameters
Section titled “Parameters”A
Returns
Section titled “Returns”Ok<A>
Example
Section titled “Example”map: <
E,A,B>(f) => (data) =>Result<E,B>
Transforms the success value inside a Result.
Type Parameters
Section titled “Type Parameters”E
A
B
Parameters
Section titled “Parameters”(a) => B
Returns
Section titled “Returns”(data) => Result<E, B>
Example
Section titled “Example”mapError
Section titled “mapError”mapError: <
E,F,A>(f) => (data) =>Result<F,A>
Transforms the error value inside a Result.
Type Parameters
Section titled “Type Parameters”E
F
A
Parameters
Section titled “Parameters”(e) => F
Returns
Section titled “Returns”(data) => Result<F, A>
Example
Section titled “Example”match: <
E,A,B>(cases) => (data) =>B
Pattern matches on a Result, returning the result of the matching case.
Type Parameters
Section titled “Type Parameters”E
A
B
Parameters
Section titled “Parameters”(e) => B
(a) => B
Returns
Section titled “Returns”(data) => B
Example
Section titled “Example”recover
Section titled “recover”recover: <
E,B>(fallback) => <A>(data) =>Result<E,B|A>
Recovers from an error by providing a fallback Result.
The fallback can produce a different success type, widening the result to Result<E, A | B>.
Type Parameters
Section titled “Type Parameters”E
B
Parameters
Section titled “Parameters”fallback
Section titled “fallback”(e) => Result<E, B>
Returns
Section titled “Returns”<A>(data) => Result<E, B | A>
recoverUnless
Section titled “recoverUnless”recoverUnless: <
E,B>(isBlocked,fallback) => <A>(data) =>Result<E,B|A>
Recovers from an error unless the predicate isBlocked returns true for that error.
The fallback can produce a different success type, widening the result to Result<E, A | B>.
Type Parameters
Section titled “Type Parameters”E
B
Parameters
Section titled “Parameters”isBlocked
Section titled “isBlocked”(e) => boolean
fallback
Section titled “fallback”() => Result<E, B>
Returns
Section titled “Returns”<A>(data) => Result<E, B | A>
Example
Section titled “Example”struct
Section titled “struct”struct: <
E,R>(fields) =>Result<E,R>
Combines a record of Results into a single Result of a record. Evaluates fields in key order and short-circuits on the first failure.
Type Parameters
Section titled “Type Parameters”E
R extends Record<string, any>
Parameters
Section titled “Parameters”fields
Section titled “fields”{ [K in string | number | symbol]: Result<E, R[K]> }
Returns
Section titled “Returns”Result<E, R>
Example
Section titled “Example”tap: <
E,A>(f) => (data) =>Result<E,A>
Executes a side effect on the success value without changing the Result. Useful for logging or debugging.
Type Parameters
Section titled “Type Parameters”E
A
Parameters
Section titled “Parameters”(a) => void
Returns
Section titled “Returns”(data) => Result<E, A>
Example
Section titled “Example”tapError
Section titled “tapError”tapError: <
E,A>(f) => (data) =>Result<E,A>
Executes a side effect on the error value without changing the Result. Useful for logging or reporting errors.
Type Parameters
Section titled “Type Parameters”E
A
Parameters
Section titled “Parameters”(e) => void
Returns
Section titled “Returns”(data) => Result<E, A>
Example
Section titled “Example”to:
object
to.Maybe
Section titled “to.Maybe”Maybe: <
E,A>(data) =>Maybe<A>
Converts a Result to a Maybe. Ok becomes Some, Err becomes None (the error is discarded).
Type Parameters
Section titled “Type Parameters”E
A
Parameters
Section titled “Parameters”Result<E, A>
Returns
Section titled “Returns”Maybe<A>
Example
Section titled “Example”to.Validation
Section titled “to.Validation”Validation: <
E,A>(data) =>Validation<E,A>
Converts a Result to a Validation. Ok(a) becomes Passed(a); Err(e) becomes Failed([e]).
Type Parameters
Section titled “Type Parameters”E
A
Parameters
Section titled “Parameters”Result<E, A>
Returns
Section titled “Returns”Validation<E, A>
Example
Section titled “Example”transposeMaybe
Section titled “transposeMaybe”Swaps the outer Result and inner Maybe context.
Ok(Some(a)) becomes Some(Ok(a)), Ok(None) becomes None, and Err(e) becomes Some(Err(e)).
Type Parameters
Section titled “Type Parameters”E
A
Parameters
Section titled “Parameters”Returns
Section titled “Returns”Example
Section titled “Example”tryCatch
Section titled “tryCatch”tryCatch: <
E,A>(f,options) =>Result<E,A>
Creates a Result from a synchronous thunk that may throw.
Catches any errors and transforms them using the onError function.
Type Parameters
Section titled “Type Parameters”E
A
Parameters
Section titled “Parameters”() => A
options
Section titled “options”onError
Section titled “onError”(e) => E
Returns
Section titled “Returns”Result<E, A>