Hyperlinkv0.8.0-beta.28

Match

Match.valueTagsconsteffect/Match.ts:360
<
  const I,
  P extends {
    readonly [Tag in Types.Tags<"_tag", I> & string]: (
      _: Extract<I, { readonly _tag: Tag }>
    ) => any
  } & { readonly [Tag in Exclude<keyof P, Types.Tags<"_tag", I>>]: never }
>(
  fields: P
): (input: I) => Unify<ReturnType<P[keyof P]>>
<
  const I,
  P extends {
    readonly [Tag in Types.Tags<"_tag", I> & string]: (
      _: Extract<I, { readonly _tag: Tag }>
    ) => any
  } & { readonly [Tag in Exclude<keyof P, Types.Tags<"_tag", I>>]: never }
>(
  input: I,
  fields: P
): Unify<ReturnType<P[keyof P]>>

Creates a match function for a specific value with discriminated union handling.

Details

This function provides a convenient way to pattern match on discriminated unions by providing an object that maps each _tag value to its corresponding handler. It's similar to a switch statement but with better type safety and exhaustiveness checking.

Example (Matching value tags)

import { Match } from "effect"

type Status = { readonly _tag: "Success"; readonly data: string }

const success: Status = { _tag: "Success", data: "Hello" }

// Simple valueTags usage
const message = Match.valueTags(success, {
  Success: (result) => `Success: ${result.data}`
})

console.log(message) // "Success: Hello"
constructors
Source effect/Match.ts:36014 lines
export const valueTags: {
  <
    const I,
    P extends
      & { readonly [Tag in Types.Tags<"_tag", I> & string]: (_: Extract<I, { readonly _tag: Tag }>) => any }
      & { readonly [Tag in Exclude<keyof P, Types.Tags<"_tag", I>>]: never }
  >(fields: P): (input: I) => Unify<ReturnType<P[keyof P]>>
  <
    const I,
    P extends
      & { readonly [Tag in Types.Tags<"_tag", I> & string]: (_: Extract<I, { readonly _tag: Tag }>) => any }
      & { readonly [Tag in Exclude<keyof P, Types.Tags<"_tag", I>>]: never }
  >(input: I, fields: P): Unify<ReturnType<P[keyof P]>>
} = internal.valueTags