variable / @typed/guard

catchTag

Recovers selected tagged typed failures and leaves unmatched tags in the error channel.

Package version
1.0.0-beta.7
Category
Error recovery
Since
1.0.0

Import

import { catchTag } from "@typed/guard";

Signatures

export declare const catchTag: {
    <const K extends Tags<E> | Arr.NonEmptyReadonlyArray<Tags<E>>, E, O2, E2, R2>(tag: K, f: (e: ExtractTag<NoInfer<E>, K extends Arr.NonEmptyReadonlyArray<string> ? K[number] : K>) => Effect.Effect<O2, E2, R2>): <I, O, R>(guard: GuardInput<I, O, E, R>) => Guard<I, O | O2, E2 | ExcludeTag<E, K extends Arr.NonEmptyReadonlyArray<string> ? K[number] : K>, R | R2>;
    <I, O, E, R, const K extends Tags<E> | Arr.NonEmptyReadonlyArray<Tags<E>>, O2, E2, R2>(guard: GuardInput<I, O, E, R>, tag: K, f: (e: ExtractTag<E, K extends Arr.NonEmptyReadonlyArray<string> ? K[number] : K>) => Effect.Effect<O2, E2, R2>): Guard<I, O | O2, E2 | ExcludeTag<E, K extends Arr.NonEmptyReadonlyArray<string> ? K[number] : K>, R | R2>;
};

Why

Tag-specific recovery preserves type-safe residual errors while lifting recovered output into Some; None, defects, and interruption are unchanged.

Ownership and lifetime

Construction acquires no resources; the handler runs only for matching tagged failures during an invocation.

Examples

import { catchTag } from "@typed/guard"
import type { Guard } from "@typed/guard"
import { Effect } from "effect"
type NotFound = { readonly _tag: "NotFound" }
const source: Guard<string, string, NotFound> = (input) => input ? Effect.succeedSome(input) : Effect.fail({ _tag: "NotFound" })
const recovered = catchTag(source, "NotFound", () => Effect.succeed("fallback"))

Source