type alias Result

The success value or a declared failure — a bare union, discriminated by the Failure brand.

E should always be a Failure type: the brand is the only discriminant, so a non-Failure E cannot be told apart from a success. It defaults to Any — the honest "some declared failure" — rather than unknown, which would collapse the union.

import * as Result from './index.ts';

const InvalidPort = Result.Failure.define('InvalidPort', (d: { raw: string }) => `not a port: ${d.raw}`);

const parsePort = (raw: string): Result.Result<number, Result.Failure.Of<typeof InvalidPort>> =>
  /^\d+$/.test(raw) ? Number(raw) : InvalidPort({ raw });

const port = parsePort('8080');
if (Result.Failure.is(port)) port.code; // narrowed to the failure here…
else port + 1; // …and to number here

Type Parameters

E = Any

Definition

T | E

Usage

import { type Result } from "lib/result/index.ts";