Result
Result:
object
Defined in: Core/Result.ts:27
Type Declaration
Section titled “Type Declaration”apply: <
E2,A>(arg) => <B,E1>(result) =>Result<E2|E1,B>
Applies a function wrapped in a Result to a value wrapped in a Result.
Type Parameters
Section titled “Type Parameters”E2 = never
A = unknown
Parameters
Section titled “Parameters”Result<E2, A>
Returns
Section titled “Returns”<B, E1>(result) => Result<E2 | E1, B>
Result.map to apply a plain unary function to an Ok value.
Example
Section titled “Example”assign
Section titled “assign”assign: <
K,E2,A,B>(key,transform) => <E1>(result) =>Result<E2|E1,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
E2
A
B
Parameters
Section titled “Parameters”K
transform
Section titled “transform”(value) => Result<E2, B>
Returns
Section titled “Returns”<E1>(result) => Result<E2 | E1, A & { [P in string]: B }>
Result.assignTo to initialize a pipeline accumulator record.
Example
Section titled “Example”assignTo
Section titled “assignTo”assignTo: <
K>(key) => <E,A>(result) =>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>(result) => Result<E, { [P in string]: A }>
Result.assign to append additional fields to an accumulator record.
Example
Section titled “Example”bimap: <
E1,E2,A,B>(onErr,onOk) => (result) =>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”(error) => E2
(value) => B
Returns
Section titled “Returns”(result) => Result<E2, B>
- Result.map to transform only the success value.
- Result.mapError to transform only the error value.
Example
Section titled “Example”chain: <
E2,A,B>(transform) => <E1>(result) =>Result<E2|E1,B>
Chains Result computations. If the first is Ok, passes the value to transform. If the first is Err, propagates the error.
Type Parameters
Section titled “Type Parameters”E2
A
B
Parameters
Section titled “Parameters”transform
Section titled “transform”(value) => Result<E2, B>
Returns
Section titled “Returns”<E1>(result) => Result<E2 | E1, B>
Result.map to transform the inner value without returning a new Result.
Example
Section titled “Example”ensure
Section titled “ensure”ensure: {<
A,B,E2>(refinement,onFail): <E1>(result) =>Result<E2|E1,B>; <A,E2>(predicate,onFail): <E1>(result) =>Result<E2|E1,A>; }
Narrows an Ok value with a predicate, converting to Err(onFail(value)) if the predicate returns false.
Call Signature
Section titled “Call Signature”<
A,B,E2>(refinement,onFail): <E1>(result) =>Result<E2|E1,B>
Type Parameters
Section titled “Type Parameters”A
B
E2
Parameters
Section titled “Parameters”refinement
Section titled “refinement”(value) => value is B
onFail
Section titled “onFail”(value) => E2
Returns
Section titled “Returns”<E1>(result) => Result<E2 | E1, B>
Call Signature
Section titled “Call Signature”<
A,E2>(predicate,onFail): <E1>(result) =>Result<E2|E1,A>
Type Parameters
Section titled “Type Parameters”A
E2
Parameters
Section titled “Parameters”predicate
Section titled “predicate”(value) => boolean
onFail
Section titled “onFail”(value) => E2
Returns
Section titled “Returns”<E1>(result) => Result<E2 | E1, A>
Result.from.Predicate to lift a value into a Result based on a predicate.
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>
Result.to.Maybe to convert a Result back into a Maybe.
Example
Section titled “Example”from.nullable
Section titled “from.nullable”nullable: <
E>(onNull) => <A>(value) =>Result<E,NonNullable<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, NonNullable<A>>
- Result.from.Maybe to convert an optional Maybe into a Result.
- Result.from.Predicate to validate and lift values into a Result based on a predicate.
Example
Section titled “Example”from.Predicate
Section titled “from.Predicate”Predicate: {<
A,B,E>(refinement,onFalse): (value) =>Result<E,B>; <A,E>(predicate,onFalse): (value) =>Result<E,A>; }
Creates a Result from a predicate or type guard applied to a value. Returns Ok if the predicate passes, Err from onFalse otherwise.
Call Signature
Section titled “Call Signature”<
A,B,E>(refinement,onFalse): (value) =>Result<E,B>
Type Parameters
Section titled “Type Parameters”A
B
E
Parameters
Section titled “Parameters”refinement
Section titled “refinement”(value) => value is B
onFalse
Section titled “onFalse”(value) => E
Returns
Section titled “Returns”(value) => Result<E, B>
Call Signature
Section titled “Call Signature”<
A,E>(predicate,onFalse): (value) =>Result<E,A>
Type Parameters
Section titled “Type Parameters”A
E
Parameters
Section titled “Parameters”predicate
Section titled “predicate”(value) => boolean
onFalse
Section titled “onFalse”(value) => E
Returns
Section titled “Returns”(value) => Result<E, A>
Result.ensure to validate and filter an existing Result.
Example
Section titled “Example”from.Validation
Section titled “from.Validation”Validation: <
E1,E2>(combineErrors) => <A>(validation) =>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
Parameters
Section titled “Parameters”combineErrors
Section titled “combineErrors”(errors) => E2
Returns
Section titled “Returns”<A>(validation) => Result<E2, A>
Result.to.Validation to convert a Result into a Validation.
Example
Section titled “Example”getOrElse
Section titled “getOrElse”getOrElse: <
B>(fallback) => <A,E>(result) =>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”fallback
Section titled “fallback”() => B
Returns
Section titled “Returns”<A, E>(result) => B | A
Result.match to handle both the Ok and Err cases.
Example
Section titled “Example”is:
object
is.err
Section titled “is.err”err: <
E,A>(result) =>result 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
Section titled “result”Result<E, A>
Returns
Section titled “Returns”result is Err<E>
Result.is.ok to check if a Result is an Ok success.
Example
Section titled “Example”ok: <
E,A>(result) =>result 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
Section titled “result”Result<E, A>
Returns
Section titled “Returns”result is Ok<A>
Result.is.err to check if a Result is an Err failure.
Example
Section titled “Example”make:
object
make.err
Section titled “make.err”err: <
E>(error) =>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: <
A,B>(transform) => <E>(result) =>Result<E,B>
Transforms the success value inside a Result.
Type Parameters
Section titled “Type Parameters”A
B
Parameters
Section titled “Parameters”transform
Section titled “transform”(value) => B
Returns
Section titled “Returns”<E>(result) => Result<E, B>
- Result.chain to sequence operations that themselves return a Result.
- Result.mapError to transform the error value instead of the success value.
Example
Section titled “Example”mapError
Section titled “mapError”mapError: <
E,F>(transform) => <A>(result) =>Result<F,A>
Transforms the error value inside a Result.
Type Parameters
Section titled “Type Parameters”E
F
Parameters
Section titled “Parameters”transform
Section titled “transform”(error) => F
Returns
Section titled “Returns”<A>(result) => Result<F, A>
Result.map to transform the success value instead of the error value.
Example
Section titled “Example”match: <
E,A,B>(cases) => (result) =>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”(error) => B
(value) => B
Returns
Section titled “Returns”(result) => B
Result.getOrElse to extract the success value with a default fallback.
Example
Section titled “Example”recover
Section titled “recover”recover: <
E1,B,E2>(fallback) => <A>(result) =>Result<E2,B|A>
Recovers from an error by providing a fallback Result. The fallback can produce a different success type or resolve with a different error type.
Type Parameters
Section titled “Type Parameters”E1
B
E2 = never
Parameters
Section titled “Parameters”fallback
Section titled “fallback”(error) => Result<E2, B>
Returns
Section titled “Returns”<A>(result) => Result<E2, B | A>
Result.recoverUnless to conditionally recover based on the error value.
Example
Section titled “Example”recoverUnless
Section titled “recoverUnless”recoverUnless: <
E1,B,E2>(isBlocked,fallback) => <A>(result) =>Result<E1|E2,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<E1 | E2, A | B>.
Type Parameters
Section titled “Type Parameters”E1
B
E2 = never
Parameters
Section titled “Parameters”isBlocked
Section titled “isBlocked”(error) => boolean
fallback
Section titled “fallback”(error) => Result<E2, B>
Returns
Section titled “Returns”<A>(result) => Result<E1 | E2, B | A>
Result.recover for unconditional error recovery.
Example
Section titled “Example”sequence
Section titled “sequence”sequence:
object
sequence.Maybe
Section titled “sequence.Maybe”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”result
Section titled “result”Returns
Section titled “Returns”Example
Section titled “Example”struct
Section titled “struct”struct: <
T>(fields) =>Result<T[keyofT] extendsResult<E,unknown> ?E:never, { [K in string | number | symbol]: T[K] extends Result<unknown, A> ? A : never }>
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”T extends Record<string, Result<unknown, unknown>>
Parameters
Section titled “Parameters”fields
Section titled “fields”T
Returns
Section titled “Returns”Result<T[keyof T] extends Result<E, unknown> ? E : never, { [K in string | number | symbol]: T[K] extends Result<unknown, A> ? A : never }>
Result.assign to combine values sequentially with inter-field dependencies.
Example
Section titled “Example”tap: <
A>(sideEffect) => <E>(result) =>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”A
Parameters
Section titled “Parameters”sideEffect
Section titled “sideEffect”(value) => void
Returns
Section titled “Returns”<E>(result) => Result<E, A>
Result.tapError to perform a side effect on the error value.
Example
Section titled “Example”tapError
Section titled “tapError”tapError: <
E>(sideEffect) => <A>(result) =>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
Parameters
Section titled “Parameters”sideEffect
Section titled “sideEffect”(error) => void
Returns
Section titled “Returns”<A>(result) => Result<E, A>
Result.tap to perform a side effect on the success value.
Example
Section titled “Example”to:
object
to.Maybe
Section titled “to.Maybe”Maybe: <
E,A>(result) =>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
Section titled “result”Result<E, A>
Returns
Section titled “Returns”Maybe<A>
Result.from.Maybe to convert a Maybe into a Result.
Example
Section titled “Example”to.Validation
Section titled “to.Validation”Validation: <
E,A>(result) =>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
Section titled “result”Result<E, A>
Returns
Section titled “Returns”Validation<E, A>
Result.from.Validation to convert a Validation into a Result.
Example
Section titled “Example”tryCatch
Section titled “tryCatch”tryCatch: <
E,A>(operation,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”operation
Section titled “operation”() => A
options
Section titled “options”onError
Section titled “onError”(error) => E
Returns
Section titled “Returns”Result<E, A>