Hyperlinkv0.8.0-beta.28

Match

Match.SafeRefinementinterfaceeffect/Match.ts:2040
SafeRefinement<A, R>

A safe refinement that narrows types without runtime errors.

Details

SafeRefinement provides a way to refine types in pattern matching while maintaining type safety. Unlike regular predicates, safe refinements can transform the matched value's type without throwing runtime errors.

Example (Using safe refinements)

import { Match } from "effect"

// Built-in safe refinements
const processValue = Match.type<unknown>().pipe(
  Match.when(Match.string, (s) => s.toUpperCase()),
  Match.when(Match.number, (n) => n * 2),
  Match.when(Match.defined, (value) => `Defined: ${value}`),
  Match.orElse(() => "Undefined or null")
)

console.log(processValue("hello")) // "HELLO"
console.log(processValue(21)) // 42
console.log(processValue(true)) // "Defined: true"
console.log(processValue(null)) // "Undefined or null"
models
Source effect/Match.ts:20403 lines
export interface SafeRefinement<in A, out R = A> {
  readonly [SafeRefinementId]: (a: A) => R
}
Referenced by 6 symbols