<A extends abstract new (...args: any) => any>(
constructor: A
): SafeRefinement<InstanceType<A>, InstanceType<A>>Checks whether a value is an instance of a constructor without type-safe narrowing.
When to use
Use when you need constructor matching to use the unsafe refinement type.
Details
This predicate checks if a value is an instance of the specified constructor
but doesn't provide the same type safety guarantees as the regular instanceOf.
Use this when you need more flexibility but understand the type safety implications.
Example (Matching class instances unsafely)
import { Match } from "effect"
class CustomError extends Error {
constructor(message: string, public code: number) {
super(message)
}
}
// When you need to match instances but handle type narrowing manually
const handleError = Match.type<unknown>().pipe(
Match.when(Match.instanceOfUnsafe(CustomError), (err: any) => {
// Manual type assertion needed
const customErr = err as CustomError
return `Custom error ${customErr.code}: ${customErr.message}`
}),
Match.orElse(() => "Not a CustomError")
)export const const instanceOfUnsafe: <
A extends abstract new (...args: any) => any
>(
constructor: A
) => SafeRefinement<
InstanceType<A>,
InstanceType<A>
>
Checks whether a value is an instance of a constructor without type-safe narrowing.
When to use
Use when you need constructor matching to use the unsafe refinement type.
Details
This predicate checks if a value is an instance of the specified constructor
but doesn't provide the same type safety guarantees as the regular instanceOf.
Use this when you need more flexibility but understand the type safety implications.
Example (Matching class instances unsafely)
import { Match } from "effect"
class CustomError extends Error {
constructor(message: string, public code: number) {
super(message)
}
}
// When you need to match instances but handle type narrowing manually
const handleError = Match.type<unknown>().pipe(
Match.when(Match.instanceOfUnsafe(CustomError), (err: any) => {
// Manual type assertion needed
const customErr = err as CustomError
return `Custom error ${customErr.code}: ${customErr.message}`
}),
Match.orElse(() => "Not a CustomError")
)
instanceOfUnsafe: <function (type parameter) A in <A extends abstract new (...args: any) => any>(constructor: A): SafeRefinement<InstanceType<A>, InstanceType<A>>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>, InstanceType<A>>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>, InstanceType<A>>A>, 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>, InstanceType<A>>A>> = import internalinternal.const instanceOf: <
A extends abstract new (...args: any) => any
>(
constructor: A
) => SafeRefinement<InstanceType<A>, never>
instanceOf