Hyperlinkv0.8.0-beta.28

Predicate

Predicate.isPropertyKeyfunctioneffect/Predicate.ts:704
(u: unknown): u is PropertyKey

Checks whether a value is a valid PropertyKey (string, number, or symbol).

When to use

Use when you need a Predicate guard for unknown property keys before indexing.

Details

Uses isString, isNumber, and isSymbol.

Example (Guarding property keys)

import { Predicate } from "effect"

const key: unknown = "name"
const obj: Record<PropertyKey, unknown> = { name: "Ada" }

if (Predicate.isPropertyKey(key) && key in obj) {
  console.log(obj[key])
}
export function isPropertyKey(u: unknown): u is PropertyKey {
  return isString(u) || isNumber(u) || isSymbol(u)
}
Referenced by 1 symbols