effect-io-ai

Package: effect
Module: Match

Match.Types.ExtractMatch

Extracts and narrows the matched type from an input type given a pattern.

Details

This is the core type utility that performs the actual type extraction and narrowing logic. It handles the complex type-level computation that determines what type results from applying a pattern to an input type.

Example (Extracting matched types)

import { Match } from "effect"

type StringExtract = Match.Types.ExtractMatch<
  string | number | boolean,
  typeof Match.string
>
// Result: string

type ObjectExtract = Match.Types.ExtractMatch<
  { type: "user"; name: string } | { type: "admin"; role: string },
  { type: "user" }
>
// Result: { type: "user"; name: string }

// This powers the type narrowing in:
Match.when(Match.string, (s) => s.toUpperCase())
//                      ^^^ s is correctly typed as string

Signature

type ExtractMatch<I, P> = [ExtractAndNarrow<I, P>] extends [infer EI] ? EI
    : never

Source

Since v4.0.0