Hyperlinkv0.8.0-beta.28

Predicate

Predicate.hasPropertyconsteffect/Predicate.ts:1131
<P extends PropertyKey>(property: P): (
  self: unknown
) => self is { [K in P]: unknown }
<P extends PropertyKey>(self: unknown, property: P): self is {
  [K in P]: unknown
}

Checks whether a value has a given property key.

When to use

Use when you need a Predicate guard for property access on unknown values with a simple structural object check.

Details

Uses the in operator and isObjectKeyword. This does not check property value types.

Example (Guarding object properties)

import { Predicate } from "effect"

const hasName = Predicate.hasProperty("name")
const data: unknown = { name: "Ada" }

if (hasName(data)) {
  console.log(data.name)
}
export const hasProperty: {
  <P extends PropertyKey>(property: P): (self: unknown) => self is { [K in P]: unknown }
  <P extends PropertyKey>(self: unknown, property: P): self is { [K in P]: unknown }
} = dual(
  2,
  <P extends PropertyKey>(self: unknown, property: P): self is { [K in P]: unknown } =>
    isObjectKeyword(self) && (property in self)
)
Referenced by 85 symbols