<
Literals extends ReadonlyArray<
string | number | bigint | boolean | null
>
>(
...literals: Literals
): SafeRefinement<Literals[number]>Matches a specific set of literal values (e.g., Match.is("a", 42, true)).
When to use
Use to match one of several literal primitive or null values.
Details
This function creates a predicate that matches any of the provided literal values. It's useful for matching against multiple specific values in a single pattern.
Example (Matching literal values)
import { Match } from "effect"
const handleStatus = Match.type<string | number>()
.pipe(
Match.when(Match.is("success", "ok", 200), () => "Operation successful"),
Match.when(Match.is("error", "failed", 500), () => "Operation failed"),
Match.when(Match.is(0, false, null), () => "Falsy value"),
Match.orElse((value) => `Unknown status: ${value}`)
)
console.log(handleStatus("success"))
// Output: "Operation successful"
console.log(handleStatus(200))
// Output: "Operation successful"
console.log(handleStatus("failed"))
// Output: "Operation failed"
console.log(handleStatus(0))
// Output: "Falsy value"
console.log(handleStatus("pending"))
// Output: "Unknown status: pending"export const const is: <
Literals extends ReadonlyArray<
string | number | bigint | boolean | null
>
>(
...literals: Literals
) => SafeRefinement<Literals[number]>
Matches a specific set of literal values (e.g., Match.is("a", 42, true)).
When to use
Use to match one of several literal primitive or null values.
Details
This function creates a predicate that matches any of the provided literal values.
It's useful for matching against multiple specific values in a single pattern.
Example (Matching literal values)
import { Match } from "effect"
const handleStatus = Match.type<string | number>()
.pipe(
Match.when(Match.is("success", "ok", 200), () => "Operation successful"),
Match.when(Match.is("error", "failed", 500), () => "Operation failed"),
Match.when(Match.is(0, false, null), () => "Falsy value"),
Match.orElse((value) => `Unknown status: ${value}`)
)
console.log(handleStatus("success"))
// Output: "Operation successful"
console.log(handleStatus(200))
// Output: "Operation successful"
console.log(handleStatus("failed"))
// Output: "Operation failed"
console.log(handleStatus(0))
// Output: "Falsy value"
console.log(handleStatus("pending"))
// Output: "Unknown status: pending"
is: <
function (type parameter) Literals in <Literals extends ReadonlyArray<string | number | bigint | boolean | null>>(...literals: Literals): SafeRefinement<Literals[number]>Literals extends interface ReadonlyArray<T>ReadonlyArray<string | number | bigint | boolean | null>
>(...literals: Literals extends ReadonlyArray<string | number | bigint | boolean | null>literals: function (type parameter) Literals in <Literals extends ReadonlyArray<string | number | bigint | boolean | null>>(...literals: Literals): SafeRefinement<Literals[number]>Literals) => interface SafeRefinement<in A, out R = A>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"
SafeRefinement<function (type parameter) Literals in <Literals extends ReadonlyArray<string | number | bigint | boolean | null>>(...literals: Literals): SafeRefinement<Literals[number]>Literals[number]> = import internalinternal.const is: <
Literals extends ReadonlyArray<
string | number | boolean | null | bigint
>
>(
...literals: Literals
) => SafeRefinement<Literals[number]>
is