tguard-js
    Preparing search index...

    Function hasKeys

    • Determines whether the provided value is an object containing all specified own property keys.

      Type Parameters

      • K extends PropertyKey

        The property key type expected to exist on the object.

      Parameters

      • o: unknown

        The value to validate.

      • keys: readonly K[]

        Required own property keys.

      • OptionalincludeHidden: boolean

        Whether to include non-enumerable and symbol keys.

      Returns o is Record<K, unknown>

      true if all specified keys exist on the object.

      This function checks whether every key in the provided keys list exists as an own property on the target object.

      It does not require the object to have exactly the same keys; additional properties are allowed.

      By default, only enumerable string keys are checked. When includeHidden is enabled, all own property keys are included, including non-enumerable and symbol keys.

      Arrays are treated as objects and are therefore supported.

      When validating arrays:

      • Object.keys() returns enumerable index keys ('0', '1', ...)
      • Reflect.ownKeys() additionally includes non-enumerable own keys such as 'length'

      This means enabling includeHidden may require including 'length' in the expected key list for arrays. See example code below:

      hasKeys([], ['length']);        // false
      hasKeys([], ['length'], true); // true
      • This function checks only own properties.
      • Prototype chain properties are ignored.
      • This function does not validate property values. Use isRecordOf for value validation.

      Basic key validation:

      const user = { id: 1, name: 'Alice' };

      hasKeys(user, ['id', 'name']); // true

      // Add new property to user
      user.email = 'alice@example.com';
      if (hasKeys(user, ['email'])) {
      // Property `email` can be used here
      }

      Missing keys:

      hasKeys(
      { id: 1 },
      ['id', 'name']
      ); // false

      Extra keys are allowed:

      hasKeys(
      { id: 1, name: 'Alice', active: true },
      ['id']
      ); // true

      Symbol keys:

      const key = Symbol('token');

      hasKeys(
      { [key]: 'abc' },
      [key],
      true
      ); // true

      1.1