import type * as Option from "./Option.ts"; import type { Pipeable } from "./Pipeable.ts"; import * as Predicate from "./Predicate.ts"; import type * as Result from "./Result.ts"; import type * as T from "./Types.ts"; import type { Unify } from "./Unify.ts"; declare const TypeId = "~effect/Match/Matcher"; type Contextual
= [P] extends [never] ? Fallback : P;
type TagHandlers = {
readonly [K in Keys]-?: undefined extends P[K] ? never : K;
}[Keys];
/**
* Marker used by `Matcher` to distinguish matchers created with `Match.value`.
*
* @stability stable
* @category models
* @since 4.0.0
*/
export type ValueFlavor = "value";
/**
* Union type for matchers created by `Match.type` and `Match.value`.
*
* **Details**
*
* A `Matcher` carries the input type, accumulated filters, remaining cases,
* result type, and a flavor distinguishing the two matcher variants: `never`
* for matchers created with `Match.type` and `ValueFlavor` for matchers created
* with `Match.value`. Because the flavor never depends on the input type,
* terminal combinators resolve even when the input contains type parameters.
*
* **Example** (Matching string and number values)
*
* ```ts import.meta.vitest
* import { Match } from "effect"
*
* // Simulated dynamic input that can be a string or a number
* const input: string | number = "some input"
*
* // ┌─── string
* // ▼
* const result = Match.value(input).pipe(
* // Match if the value is a number
* Match.when(Match.number, (n) => `number: ${n}`),
* // Match if the value is a string
* Match.when(Match.string, (s) => `string: ${s}`),
* // Ensure all possible cases are covered
* Match.exhaustive
* )
*
* result // => "string: some input"
* ```
*
* @stability stable
* @category models
* @since 4.0.0
*/
export type Matcher = []> = TypeMatcher | ValueMatcher;
/**
* Represents a pattern matcher that operates on types rather than specific values.
*
* **Details**
*
* A `TypeMatcher` is created when using `Match.type >): (input: I) => Unify >): Unify & string]: (_: Extract) => Ret;
} & {
readonly [Tag in Exclude & string]: (_: Extract) => any;
} & {
readonly [Tag in Exclude , f: Fn]) => (self: Matcher) => Matcher>>, Types.ApplyFilters>>>, A | ReturnType >) => (self: Matcher) => Matcher>>>, Types.ApplyFilters>>>>, A | ReturnType , Pr, Ret>;
/**
* Matches values by a discriminator field and requires every possible case to
* be handled.
*
* **When to use**
*
* Use to define an exhaustive discriminator handler map that finalizes the
* matcher.
*
* **Details**
*
* This is the exhaustive variant of {@link discriminators}. Each possible
* discriminator value must have a corresponding handler, so the matcher is
* finalized directly and does not require `Match.exhaustive` at the end of the
* pipeline.
*
* **Example** (Handling all discriminator cases)
*
* ```ts import.meta.vitest
* import { Match, pipe } from "effect"
*
* const match = pipe(
* Match.type<
* { type: "A"; a: string } | { type: "B"; b: number } | {
* type: "C"
* c: boolean
* }
* >(),
* Match.discriminatorsExhaustive("type")({
* A: (a) => a.a,
* B: (b) => b.b,
* C: (c) => c.c
* })
* )
* match({ type: "C", c: true }) // => true
* ```
*
* @see {@link discriminators} for defining discriminator handlers without finalizing the matcher
*
* @stability stable
* @category defining patterns
* @since 4.0.0
*/
export declare const discriminatorsExhaustive: >) => (self: Matcher) => [Pr] extends [never] ? (u: I) => Unify> : Unify>;
/**
* Matches discriminated union members by their `_tag` field.
*
* **When to use**
*
* Use to handle one or more `_tag` cases with the same matcher branch.
*
* **Details**
*
* This helper follows the Effect convention that discriminated unions use
* `"_tag"` as their discriminator field. Use {@link discriminator} for a
* different discriminator field.
*
* **Example** (Matching a discriminated union by tag)
*
* ```ts import.meta.vitest
* import { Match } from "effect"
*
* type Event =
* | { readonly _tag: "fetch" }
* | { readonly _tag: "success"; readonly data: string }
* | { readonly _tag: "error"; readonly error: Error }
* | { readonly _tag: "cancel" }
*
* const match = Match.type , f: Fn]) => (self: Matcher) => Matcher>>, Types.ApplyFilters>>>, ReturnType >) => (self: Matcher) => Matcher>>>, Types.ApplyFilters>>>>, A | ReturnType , Pr, Ret>;
/**
* Matches values based on their `_tag` field and requires handling of all
* possible cases.
*
* **Details**
*
* This function is designed for **discriminated unions** where every possible
* `_tag` value must have a corresponding handler. Unlike {@link tags}, this
* function ensures **exhaustiveness**, meaning all cases must be explicitly
* handled. If a `_tag` value is missing from the mapping, TypeScript will
* report an error.
*
* **Example** (Handling all tag cases)
*
* ```ts import.meta.vitest
* import { Match, pipe } from "effect"
*
* const match = pipe(
* Match.type<
* { _tag: "A"; a: string } | { _tag: "B"; b: number } | {
* _tag: "C"
* c: boolean
* }
* >(),
* Match.tagsExhaustive({
* A: (a) => a.a,
* B: (b) => b.b,
* C: (c) => c.c
* })
* )
* match({ _tag: "B", b: 42 }) // => 42
* ```
*
* @stability stable
* @category defining patterns
* @since 4.0.0
*/
export declare const tagsExhaustive: >) => (self: Matcher) => [Pr] extends [never] ? (u: I) => Unify> : Unify>;
/**
* Matches string and number literal values, mapping each literal to a
* corresponding handler.
*
* **Details**
*
* Each handler receives the matched literal. Handled literals are removed from
* the remaining input, while unhandled literals and any other members (such as
* `null`, objects, or widened `string` / `number` types) are left for later
* cases. Keys must be finite string or number literals of the remaining input;
* unions containing both a number and its string spelling, such as
* `1 | "1"`, are rejected because both share the same runtime key.
*
* **Example** (Mapping literal handlers)
*
* ```ts import.meta.vitest
* import { Match } from "effect"
*
* type Status = "draft" | "review" | "published"
*
* const isEditable = Match.type >) => (self: Matcher) => Matcher>>, Types.ApplyFilters>>>, A | ReturnType , Pr, Ret>;
/**
* Matches string and number literal values and requires handling of all
* remaining cases.
*
* **Details**
*
* This is the exhaustive variant of {@link literals}. Every remaining member
* must be a finite string or number literal with a corresponding handler, so a
* missing literal, a widened `string` / `number` type, or any other unhandled
* member is reported by TypeScript.
*
* **Example** (Handling all literal cases)
*
* ```ts import.meta.vitest
* import { Match } from "effect"
*
* type Status = "draft" | "review" | "published"
*
* const label = Match.type >) => (self: Matcher) => [Pr] extends [never] ? (u: I) => Unify> : Unify>;
/**
* Creates a pattern that excludes a specific value while allowing all others.
*
* **When to use**
*
* Use to add a negative pattern case for inputs that should match when another
* pattern does not.
*
* **Details**
*
* Any excluded value bypasses the provided function and continues matching
* through later cases.
* Object patterns, including nested patterns, use only own enumerable string
* and symbol properties. Non-enumerable and inherited pattern properties are
* ignored. Selected properties on the input may be non-enumerable or inherited.
*
* **Example** (Ignoring a specific value)
*
* ```ts import.meta.vitest
* import { Match } from "effect"
*
* // Create a matcher for string or number values
* const match = Match.type : P extends SafeRefinement