type alias Result
Since 0.1.0

The result of an operation that can fail: either Ok or Err. Narrow with the success discriminant or the isOk/isErr guards.

Examples

Example 1

function parsePort(raw: string): Result<number, RangeError> {
  const port = Number(raw);
  return Number.isInteger(port) && port > 0 && port < 65536
    ? ok(port)
    : err(new RangeError(`Invalid port: ${raw}`));
}

const result = parsePort('3000');
if (result.success) {
  listen(result.value);
}

Type Parameters

The success value type

E = Error

The error type (defaults to Error)

Definition

Ok<T> | Err<E>

Usage

import { type Result } from "common/src/index.ts";