Skip to main content

Type Guards

isOk()

Returns true if the result is Ok, narrowing the type in TypeScript.

Returns

boolean
true for Ok instances, false for Err instances

Example

isErr()

Returns true if the result is Err, narrowing the type in TypeScript.

Returns

boolean
true for Err instances, false for Ok instances

Example

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 fn to the value, returns new Ok with result
  • On Err: Returns self unchanged (no-op)
  • Throws: Panic if fn throws

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 fn to the error, returns new Err with result
  • Throws: Panic if fn throws

Examples

Chaining

andThen()

Chains a Result-returning function on success (also known as flatMap 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 type

Behavior

  • On Ok: Calls fn with the value, returns its Result
  • On Err: Returns self with widened error type (no-op)
  • Throws: Panic if fn throws

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 unchanged

Behavior

  • On Ok: Awaits fn with the value, returns its Result
  • On Err: Returns Promise of self with widened error type (no-op)
  • Throws: Panic if fn throws synchronously or rejects

Examples

Pattern Matching

match()

Pattern matches on the Result, executing the appropriate handler.

Parameters

object
required
Object with ok and err handler functions
(a: A) => T
required
Handler called if Result is Ok, receives the value
(e: E) => T
required
Handler called if Result is Err, receives the error

Returns

T
The return value of the executed handler

Behavior

  • On Ok: Executes handlers.ok with the value
  • On Err: Executes handlers.err with the error
  • Throws: Panic if 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 Panic with optional custom message
  • Throws: Always throws on Err
Only use unwrap() when you’re certain the Result is Ok, or in contexts where throwing is acceptable (e.g., tests). For production code, prefer unwrapOr() or match().

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 fn with the value, returns self
  • On Err: No-op, returns self
  • Throws: Panic if fn throws

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 fn with the value, returns Promise of self
  • On Err: No-op, returns Promise of self
  • Throws: Panic if fn throws synchronously or rejects

Examples

Generator Protocol

[Symbol.iterator]()

Makes Results yieldable in Result.gen() blocks.

Behavior

  • On Ok: Returns the value immediately without yielding
  • On Err: Yields the error and short-circuits the generator
This enables the yield* syntax in generator-based composition:
See Result.gen() for more details.

See Also