Library API
    Preparing search index...

    Function hasKey

    • Asserts that an object contains a specified key, optionally validating and narrowing its value type. Safe against null, undefined, and non-object primitives.

      Type Parameters

      • K extends PropertyKey

      Parameters

      • obj: unknown

        The target object to inspect

      • key: K

        The property key to verify, including inherited properties

      Returns obj is Record<K, unknown>

      True if the target contains the key

      if (hasKey(config, 'timeout')) { ... }
      
    • Asserts that an object contains a specified key whose value matches at least one of the specified type tags (case-insensitive, e.g. 'string', 'String', 'Number') or predicate functions.

      Type Parameters

      • K extends PropertyKey
      • G extends AnyGuard[]

      Parameters

      • obj: unknown

        The target object to inspect

      • key: K

        The property key to verify

      • ...guards: [...G[]]

        One or more type tags or predicate functions

      Returns obj is Record<K, ResolveTypeGuards<G>>

      True if the target contains the key and its value satisfies at least one guard

      if (hasKey(err, 'code', 'String', 'number')) {
      console.log(err.code); // string | number
      }
      if (hasKey(err, 'code', isString, isNumber)) {
      console.log(err.code); // string | number
      }