Type Guards
isOk()
Returnstrue if the result is Ok, narrowing the type in TypeScript.
Returns
boolean
true for Ok instances, false for Err instancesExample
isErr()
Returnstrue if the result is Err, narrowing the type in TypeScript.
Returns
boolean
true for Err instances, false for Ok instancesExample
Transformation
map()
Transforms the success value if Ok, passes through if Err.Parameters
(a: A) => B
required
Transformation function applied to the success value
Returns
Result<B, E>
Ok with transformed value if original was Ok, otherwise Err unchanged
Behavior
- On Ok: Applies
fnto the value, returns new Ok with result - On Err: Returns self unchanged (no-op)
- Throws:
Paniciffnthrows
Examples
mapError()
Transforms the error value if Err, passes through if Ok.Parameters
(e: E) => E2
required
Transformation function applied to the error value
Returns
Result<A, E2>
Err with transformed error if original was Err, otherwise Ok unchanged
Behavior
- On Ok: Returns self with updated phantom error type (no-op at runtime)
- On Err: Applies
fnto the error, returns new Err with result - Throws:
Paniciffnthrows
Examples
Chaining
andThen()
Chains a Result-returning function on success (also known asflatMap or bind).
Parameters
(a: A) => Result<B, E2>
required
Function that returns a new Result
Returns
Result<B, E | E2>
The Result returned by
fn if original was Ok, otherwise Err unchanged with widened error typeBehavior
- On Ok: Calls
fnwith the value, returns its Result - On Err: Returns self with widened error type (no-op)
- Throws:
Paniciffnthrows
Examples
andThenAsync()
Chains an async Result-returning function on success.Parameters
(a: A) => Promise<Result<B, E2>>
required
Async function that returns a Promise of Result
Returns
Promise<Result<B, E | E2>>
Promise of the Result returned by
fn if original was Ok, otherwise Err unchangedBehavior
- On Ok: Awaits
fnwith the value, returns its Result - On Err: Returns Promise of self with widened error type (no-op)
- Throws:
Paniciffnthrows synchronously or rejects
Examples
Pattern Matching
match()
Pattern matches on the Result, executing the appropriate handler.Parameters
object
required
Returns
T
The return value of the executed handler
Behavior
- On Ok: Executes
handlers.okwith the value - On Err: Executes
handlers.errwith the error - Throws:
Panicif handler throws
Examples
Unwrapping
unwrap()
Extracts the value from Ok or throws an error.Parameters
string
Custom error message if called on Err
Returns
A
The success value if Ok
Behavior
- On Ok: Returns the value
- On Err: Throws
Panicwith optional custom message - Throws: Always throws on Err
Examples
unwrapOr()
Extracts the value from Ok or returns a fallback value.Parameters
B
required
Value to return if Result is Err
Returns
A | B
The success value if Ok, otherwise the fallback
Behavior
- On Ok: Returns the value, ignores fallback
- On Err: Returns the fallback
- Never throws
Examples
Side Effects
tap()
Runs a side effect function on success, returns the original Result.Parameters
(a: A) => void
required
Side effect function to execute with the value
Returns
Result<A, E>
The original Result unchanged
Behavior
- On Ok: Executes
fnwith the value, returns self - On Err: No-op, returns self
- Throws:
Paniciffnthrows
Examples
tapAsync()
Runs an async side effect function on success, returns Promise of the original Result.Parameters
(a: A) => Promise<void>
required
Async side effect function to execute with the value
Returns
Promise<Result<A, E>>
Promise of the original Result unchanged
Behavior
- On Ok: Awaits
fnwith the value, returns Promise of self - On Err: No-op, returns Promise of self
- Throws:
Paniciffnthrows synchronously or rejects
Examples
Generator Protocol
[Symbol.iterator]()
Makes Results yieldable inResult.gen() blocks.
Behavior
- On Ok: Returns the value immediately without yielding
- On Err: Yields the error and short-circuits the generator
yield* syntax in generator-based composition: