Hyperlinkv0.8.0-beta.28

Match

Match.typeconsteffect/Match.ts:281
<I>(): Matcher<I, Types.Without<never>, I, never, never>

Creates a matcher for a specific type.

When to use

Use to build a reusable matcher function for values of a known input type.

Details

This function defines a Matcher that operates on a given type, allowing you to specify conditions for handling different cases. Once the matcher is created, you can use pattern-matching functions like when to define how different values should be processed.

Example (Matching Numbers and Strings)

import { Match } from "effect"

// Create a matcher for values that are either strings or numbers
//
//      ┌─── (u: string | number) => string
//      ▼
const match = Match.type<string | number>().pipe(
  // Match when the value is a number
  Match.when(Match.number, (n) => `number: ${n}`),
  // Match when the value is a string
  Match.when(Match.string, (s) => `string: ${s}`),
  // Ensure all possible cases are handled
  Match.exhaustive
)

console.log(match(0))
// Output: "number: 0"

console.log(match("hello"))
// Output: "string: hello"
constructorswhenvalue
Source effect/Match.ts:2811 lines
export const type: <I>() => Matcher<I, Types.Without<never>, I, never, never> = internal.type
Referenced by 1 symbols