Hyperlinkv0.8.0-beta.28

Match

Match.recordconsteffect/Match.ts:1677
(a: unknown): a is {
  [x: string]: unknown
  [x: number]: unknown
  [x: symbol]: unknown
}

Matches non-null objects other than arrays.

When to use

Use to match broad non-null, non-array object values.

Details

This predicate uses Predicate.isObject: it returns true for values whose runtime type is "object", are not null, and are not arrays. It can match Date, RegExp, and class instances; use instanceOf or a more specific pattern when those cases need to be distinguished.

Example (Matching record objects)

import { Match } from "effect"

const analyzeValue = Match.type<unknown>().pipe(
  Match.when(Match.record, (obj) => {
    const keys = Object.keys(obj)
    const valueCount = keys.length
    return `Object with ${valueCount} properties: [${keys.join(", ")}]`
  }),
  Match.when(
    Match.instanceOf(Array),
    (arr) => `Array with ${arr.length} items`
  ),
  Match.orElse(() => "Not an object")
)

console.log(analyzeValue({ name: "Alice", age: 30 })) // "Object with 2 properties: [name, age]"
console.log(analyzeValue([1, 2, 3])) // "Array with 3 items"
console.log(analyzeValue(null)) // "Not an object"
console.log(analyzeValue("hello")) // "Not an object"
predicatesinstanceOf
Source effect/Match.ts:16771 lines
export const record: Predicate.Refinement<unknown, { [x: PropertyKey]: unknown }> = Predicate.isObject