Function isKeyOfObject

  • Utility for checking if a key exists in an object. Note this is a type guard, and will inform typescript that the key definitely exists in that object.

    This uses Object.hasOwn, which only checks for keys that exist as it's own property, not inherited properties.

    Returns

    a boolean indicating whether the key exists

    Example

    isKeyOfObject('name', { name: 'John' }) === true
    

    Example

    isKeyOfObject('toString', { name: 'John' }) === false
    

    Example

    isKeyOfObject('age', { name: 'John' }) === false
    

    See

    MDN reference for Object.hasOwn

    Type Parameters

    • T extends object

    Parameters

    • key: PropertyKey

      the key that may or may not exist

    • obj: T

      the object to check

    Returns key is keyof T

Generated using TypeDoc