variable / @typed/async-data

match

Exhaustively folds every AsyncData variant, in data-first or data-last form.

Package version
1.0.0-beta.7
Category
Pattern matching
Since
1.0.0

Import

import { match } from "@typed/async-data";

Signatures

export declare const match: {
    <A, E, R1, R2, R3, R4, R5>(matchers: {
        NoData: (data: NoData) => R1;
        Loading: (data: Loading) => R2;
        Failure: (cause: Cause.Cause<E>, data: Failure<E>) => R3;
        Success: (value: A, data: Success<A>) => R4;
        Optimistic: (value: A, data: Optimistic<A, E>) => R5;
    }): (data: AsyncData<A, E>) => Unify<R1 | R2 | R3 | R4 | R5>;
    <A, E, R1, R2, R3, R4, R5>(data: AsyncData<A, E>, matchers: {
        NoData: (data: NoData) => R1;
        Loading: (data: Loading) => R2;
        Failure: (cause: Cause.Cause<E>, data: Failure<E>) => R3;
        Success: (value: A, data: Success<A>) => R4;
        Optimistic: (value: A, data: Optimistic<A, E>) => R5;
    }): Unify<R1 | R2 | R3 | R4 | R5>;
};

Why

Centralized exhaustive dispatch exposes values and Causes with their full state while TypeScript unifies branch result types.

Ownership and lifetime

Matching is synchronous, acquires no resources, and retains nothing beyond callback behavior.

Examples

import { match } from "@typed/async-data"
const label = match({ NoData: () => "empty", Loading: () => "loading", Failure: () => "failed", Success: String, Optimistic: String })

Source