Skip to content

Result

Result is a discriminated union of success and failure values.

ts
type Result<T, E = unknown> = Ok<T, E> | Err<E, T>

Narrow with ok

ts
function print(result: Result<number, Error>) {
  if (result.ok) {
    console.log(result.value)
    return
  }

  console.error(result.error.message)
}

Available methods

Every Result supports:

The Ok variant has ok: true and value. The Err variant has ok: false and error.