<A extends abstract new (...args: any) => any>(
constructor: A
): SafeRefinement<InstanceType<A>, never>Matches instances of a given class.
When to use
Use to match values that are instances of a constructor with type-safe narrowing.
Details
This predicate checks if a value is an instance of the specified constructor, providing type-safe matching for class instances and built-in objects.
Example (Matching class instances)
import { Match } from "effect"
class CustomError extends Error {
constructor(message: string, public code: number) {
super(message)
}
}
const handleValue = Match.type<unknown>()
.pipe(
Match.when(
Match.instanceOf(CustomError),
(err) => `Custom error: ${err.message} (code: ${err.code})`
),
Match.when(
Match.instanceOf(Error),
(err) => `Standard error: ${err.message}`
),
Match.when(
Match.instanceOf(Array),
(arr) => `Array with ${arr.length} items`
),
Match.when(
Match.instanceOf(Map),
(map) => `Map with ${map.size} entries`
),
Match.orElse((value) => `Other: ${typeof value}`)
)
console.log(handleValue(new CustomError("Failed", 404))) // "Custom error: Failed (code: 404)"
console.log(handleValue(new Error("Generic error"))) // "Standard error: Generic error"
console.log(handleValue([1, 2, 3])) // "Array with 3 items"
console.log(handleValue(new Map([["count", 1]]))) // "Map with 1 entries"export const const instanceOf: <
A extends abstract new (...args: any) => any
>(
constructor: A
) => SafeRefinement<InstanceType<A>, never>
Matches instances of a given class.
When to use
Use to match values that are instances of a constructor with type-safe
narrowing.
Details
This predicate checks if a value is an instance of the specified constructor,
providing type-safe matching for class instances and built-in objects.
Example (Matching class instances)
import { Match } from "effect"
class CustomError extends Error {
constructor(message: string, public code: number) {
super(message)
}
}
const handleValue = Match.type<unknown>()
.pipe(
Match.when(
Match.instanceOf(CustomError),
(err) => `Custom error: ${err.message} (code: ${err.code})`
),
Match.when(
Match.instanceOf(Error),
(err) => `Standard error: ${err.message}`
),
Match.when(
Match.instanceOf(Array),
(arr) => `Array with ${arr.length} items`
),
Match.when(
Match.instanceOf(Map),
(map) => `Map with ${map.size} entries`
),
Match.orElse((value) => `Other: ${typeof value}`)
)
console.log(handleValue(new CustomError("Failed", 404))) // "Custom error: Failed (code: 404)"
console.log(handleValue(new Error("Generic error"))) // "Standard error: Generic error"
console.log(handleValue([1, 2, 3])) // "Array with 3 items"
console.log(handleValue(new Map([["count", 1]]))) // "Map with 1 entries"
instanceOf: <function (type parameter) A in <A extends abstract new (...args: any) => any>(constructor: A): SafeRefinement<InstanceType<A>, never>A extends abstract new(...args: anyargs: any) => any>(
constructor: A extends abstract new (...args: any) => anyconstructor: function (type parameter) A in <A extends abstract new (...args: any) => any>(constructor: A): SafeRefinement<InstanceType<A>, never>A
) => 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<type InstanceType<
T extends abstract new (...args: any) => any
> = T extends abstract new (
...args: any
) => infer R
? R
: any
Obtain the return type of a constructor function type
InstanceType<function (type parameter) A in <A extends abstract new (...args: any) => any>(constructor: A): SafeRefinement<InstanceType<A>, never>A>, never> = import internalinternal.const instanceOf: <
A extends abstract new (...args: any) => any
>(
constructor: A
) => SafeRefinement<InstanceType<A>, never>
instanceOf