Hyperlinkv0.8.0-beta.28

Match

Match.tagsconsteffect/Match.ts:1041
<
  R,
  Ret,
  P extends {
    readonly [Tag in Types.Tags<"_tag", R> & string]?:
      | ((_: Extract<R, Record<"_tag", Tag>>) => Ret)
      | undefined
  } & { readonly [Tag in Exclude<keyof P, Types.Tags<"_tag", R>>]: never }
>(
  fields: P
): <I, F, A, Pr>(
  self: Matcher<I, F, R, A, Pr, Ret>
) => Matcher<
  I,
  Types.AddWithout<F, Extract<R, Record<"_tag", keyof P>>>,
  Types.ApplyFilters<
    I,
    Types.AddWithout<F, Extract<R, Record<"_tag", keyof P>>>
  >,
  A | ReturnType<P[keyof P] & {}>,
  Pr,
  Ret
>

Matches values based on their _tag field, mapping each tag to a corresponding handler.

Details

This function provides a way to handle discriminated unions by mapping _tag values to specific functions. Each handler receives the matched value and returns a transformed result. If all possible tags are handled, you can enforce exhaustiveness using Match.exhaustive to ensure no case is missed.

Example (Mapping tag handlers)

import { Match, pipe } from "effect"

const match = pipe(
  Match.type<
    { _tag: "A"; a: string } | { _tag: "B"; b: number } | {
      _tag: "C"
      c: boolean
    }
  >(),
  Match.tags({
    A: (a) => a.a,
    B: (b) => b.b,
    C: (c) => c.c
  }),
  Match.exhaustive
)
Defining patterns
Source effect/Match.ts:104118 lines
export const tags: <
  R,
  Ret,
  P extends
    & { readonly [Tag in Types.Tags<"_tag", R> & string]?: ((_: Extract<R, Record<"_tag", Tag>>) => Ret) | undefined }
    & { readonly [Tag in Exclude<keyof P, Types.Tags<"_tag", R>>]: never }
>(
  fields: P
) => <I, F, A, Pr>(
  self: Matcher<I, F, R, A, Pr, Ret>
) => Matcher<
  I,
  Types.AddWithout<F, Extract<R, Record<"_tag", keyof P>>>,
  Types.ApplyFilters<I, Types.AddWithout<F, Extract<R, Record<"_tag", keyof P>>>>,
  A | ReturnType<P[keyof P] & {}>,
  Pr,
  Ret
> = internal.tags
Referenced by 1 symbols