Skip to main content

keys()

keys<T>(object): keyof T[]

keys(object): []

keys(object): string[]

Creates an array of the own enumerable property names of an object.

DEPRECATED

Use Object.keys() directly instead.


Type Parameters​

T: T extends object​

The type of the object.


Parameters​

Overload 1:

object: T​

The object to query.

Overload 2:

object: null | undefined​

The object to query.

Overload 3:

object: unknown​

The object to query.


Returns: keyof T[]​

An array of property names.


See Also​


Since​

2.0.0


Also known as​

keys (Lodash, es-toolkit, Remeda, Radashi, Ramda, Effect) · objectKeys (Antfu) · ❌ (Modern Dash)


Example​

const obj = { a: 1, b: 2, c: 3 };

// ❌ Deprecated approach
const objKeys = keys(obj);
console.log(objKeys); // ['a', 'b', 'c']

// βœ… Recommended approach
const nativeKeys = Object.keys(obj);
console.log(nativeKeys); // ['a', 'b', 'c']

How it works?​

Creates an array of own enumerable property names. Deprecated: Use Object.keys() directly (ES5).

Native Equivalent​

// ❌ keys(obj)
// βœ… Object.keys(obj)

Use Cases​

Get object keys πŸ“Œβ€‹

Get all property names.

Object.keys(obj);     // => ["a", "b", "c"]
Object.values(obj); // => [1, 2, 3]
Object.entries(obj); // => [["a", 1], ["b", 2], ["c", 3]]

Iterate properties​

Loop over object properties.

for (const [key, value] of Object.entries(obj)) {
console.log(key, value);
}

Transform object​

Transform using entries.

Object.fromEntries(
Object.entries(obj).map(([k, v]) => [k, v * 2])
);

Count object properties for validation​

Check the number of properties in an object to enforce limits. Useful for API payload validation, configuration checks, and form field counting.

const payload = { name: "Alice", email: "alice@example.com", role: "admin" };

const fieldCount = keys(payload).length;
// => 3

if (fieldCount > 50) {
throw new Error("Payload too large: max 50 fields allowed");
}