Skip to content

hasKeys()

ts
function hasKeys(this, ...keys): this is PickFromKeys<object, readonly never[]>;

Defined in: object/has-keys.ts:21

Checks whether an object has all of the specified keys, and acts as a TypeScript type guard that narrows the type of the object to only include those keys

Parameters

ParameterTypeDescription
thisobjectobject instance
...keysreadonly never[]The keys to check for in the object

Returns

this is PickFromKeys<object, readonly never[]>

true if all specified keys are present in the object. Otherwise false. When true, TypeScript narrows the type of this to PickFromKeys<this, typeof keys> (the object with at least those keys)

Example

ts
const hasKeysObject = { name: "john", age: 39 };
console.assert(hasKeysObject.hasKeys("name"));
console.assert(hasKeysObject.hasKeys("age"));

Since

1.0.0

Author

Simon Kovtyk