Skip to content

Type Alias: Result<T, E>

Result<T, E> = Result<T, E>

Defined in: src/index.ts:59

Utilities for creating and composing Result values.

Type Parameters

Type Parameter
T
E

See

Result<T, E> represents either:

  • Ok — success containing T
  • Err — failure containing E

Inspired by Rust's Result<T, E>, enabling explicit error handling without exceptions.

Remarks

Runtime namespace with helpers such as:

  • Result.ok(value)
  • Result.err(error)
  • Result.fromTry(fn)
  • Result.fromPromise(fn)
  • Result.fromNullable(value)
  • Result.all(results)

Also exported as a type alias:

Examples

ts
function divide(a: number, b: number): Result<number, string> {
  if (b === 0) return Result.err('division by zero')
  return Result.ok(a / b)
}
ts
const value = Result
  .ok(10)
  .map((x) => x * 2)
  .andThen((x) => Result.ok(x + 5))
// => Ok(25)
ts
const parsed = Result.fromTry(
  () => JSON.parse(input)
)
// => Ok(parsed) | Err(Error)
ts
const user = await Result.fromPromise(
  () => fetchUser(id)
)
// => Ok(user) | Err(Error)