function isFailure
isFailure(value: unknown): value is Any

Whether value is a Failure — from this realm or any other.

The brand lookup is the primary test. The structural fallback covers the one case a brand cannot: toJSON output revived by something other than fromJSON — a JSON round trip through a cache, a WebSocket frame handed straight to application code — which keeps the fields but loses both the prototype and the non-enumerable brand.

Note what is deliberately not covered: structuredClone(failure). The structured clone algorithm handles Error objects by preserving name, message, stack and cause and discarding both the subclass and every own property — so code and data are simply gone, and answering true here would be a lie. Serialize with toJSON, not structuredClone.

import * as Failure from './failure.ts';

const work = async (): Promise<void> => {};
const report = (code: string, data: unknown): void => console.debug(code, data);

try {
  await work();
} catch (error) {
  if (!Failure.is(error)) throw error; // a bug keeps flying
  report(error.code, error.data); // a declared failure gets handled
}

Parameters

value: unknown

Return Type

value is Any

Usage

import { isFailure } from "lib/result/failure.ts";