FuncType - v0.45.0
    Preparing search index...

    Type Alias Match<T, R>

    Pattern matching construct similar to Scala's match expressions. Supports exhaustive matching, nested patterns, and guards.

    // Basic pattern matching
    const result = Match(value)
    .case(x => x > 100, "large")
    .case(x => x > 50, "medium")
    .default("small")
    // Matching exact values
    const message = Match(status)
    .caseValue("pending", "Please wait...")
    .caseValue("success", "Completed!")
    .caseValue("error", "Failed")
    .default("Unknown")
    // Nested pattern matching
    const user = { name: "John", age: 30, role: "admin" }
    const msg = Match(user)
    .case({ role: "admin", age: n => n >= 18 }, "Adult admin")
    .case({ role: "user" }, u => `User: ${u.name}`)
    .default("Guest")
    type Match<T extends Type, R extends Type> = {
        case: <R2 extends Type>(
            pattern: Pattern<T>,
            result: PatternResult<T, R2>,
        ) => Match<T, R | R2>;
        caseAny: <R2 extends Type>(
            patterns: Pattern<T>[],
            result: PatternResult<T, R2>,
        ) => Match<T, R | R2>;
        caseValue: <R2 extends Type>(
            match: T,
            result: R2 | (() => R2),
        ) => Match<T, R | R2>;
        caseValues: <R2 extends Type>(
            matches: T[],
            result: R2 | (() => R2),
        ) => Match<T, R | R2>;
        default: <R2 extends Type>(result: PatternResult<T, R2>) => R | R2;
        exhaustive: () => R;
        orThrow: (errorMessage?: string) => R;
        toOption: () => Option<R>;
        when: <R2 extends Type>(
            guard: (value: T) => boolean,
            result: PatternResult<T, R2>,
        ) => Match<T, R | R2>;
    }

    Type Parameters

    Index

    Properties

    case: <R2 extends Type>(
        pattern: Pattern<T>,
        result: PatternResult<T, R2>,
    ) => Match<T, R | R2>

    Match against a pattern (value, nested object, or predicate). The result type R2 is added to the union of possible results.

    caseAny: <R2 extends Type>(
        patterns: Pattern<T>[],
        result: PatternResult<T, R2>,
    ) => Match<T, R | R2>

    Match multiple patterns (OR operation). The result type R2 is added to the union of possible results.

    caseValue: <R2 extends Type>(
        match: T,
        result: R2 | (() => R2),
    ) => Match<T, R | R2>

    Add a case that matches a specific value. The result type R2 is added to the union of possible results.

    caseValues: <R2 extends Type>(
        matches: T[],
        result: R2 | (() => R2),
    ) => Match<T, R | R2>

    Add a case that matches multiple values. The result type R2 is added to the union of possible results.

    default: <R2 extends Type>(result: PatternResult<T, R2>) => R | R2

    Default case - makes match non-exhaustive. The result type R2 is added to the union of possible results.

    exhaustive: () => R

    Force exhaustive matching (compile-time check for union types)

    orThrow: (errorMessage?: string) => R

    Get result if matched, throws if no match

    toOption: () => Option<R>

    Get result wrapped in Option

    when: <R2 extends Type>(
        guard: (value: T) => boolean,
        result: PatternResult<T, R2>,
    ) => Match<T, R | R2>

    Match with a guard function (alias for readability). The result type R2 is added to the union of possible results.