Extract Object Keys
extractObjectKeys
Extracts the keys of an object with proper typing.
Import
import { extractObjectKeys } from 'nhb-toolbox';
Function Signatures
extractObjectKeys<T extends GenericObject>(obj: T): Array<keyof T>;
extractObjectKeys<T extends GenericObject>(obj: T, tuple: true): Tuple<keyof T>;
Aliases
extractKeys
- Alias forextractObjectKeys
Usage
Basic Usage
const obj = { a: 1, b: 2, c: 3 };
const keys = extractObjectKeys(obj); // Returns ['a', 'b', 'c']
With Tuple Return Type
const obj = { a: 1, b: 2, c: 3 };
const keys = extractObjectKeys(obj, true); // Returns ['a', 'b', 'c'] as Tuple
With Empty Object
TypeScript will complain and it will return empty array.
const keys = extractObjectKeys({}); // Returns []
With Null/Undefined
TypeScript will complain and it will return empty array.
const keys = extractObjectKeys(null); // Returns []
const keys = extractObjectKeys(undefined); // Returns []
API
Type Parameters
Parameter | Description |
---|---|
T | A generic object type |
Parameters
Parameter | Type | Description |
---|---|---|
obj | T | The object to extract keys from |
tuple | boolean | Optional. If true , returns keys as tuple instead of an array. |
Return Value
- Without
tuple
:Array<keyof T>
- An array of keys from the specified object - With
tuple: true
:Tuple<keyof T>
- A tuple of keys from the specified object - Returns empty array/tuple if the object is null/undefined/empty
Examples
- Simple Object
- With Tuple
- Nested Object
- Edge Cases
const user = {
id: 1,
name: 'John Doe',
email: 'john@example.com'
};
const keys = extractObjectKeys(user); // ['id', 'name', 'email']
const user = {
id: 1,
name: 'John Doe',
email: 'john@example.com'
};
const keys = extractObjectKeys(user, true); // ['id', 'name', 'email'] as Tuple
const product = {
id: 1,
name: 'Laptop',
details: {
brand: 'Dell',
price: 999
}
};
const keys = extractObjectKeys(product); // ['id', 'name', 'details']
// Note: Only top-level keys are returned
TypeScript will complain and it will return empty array.
// Empty object
extractObjectKeys({}); // []
// Null case
extractObjectKeys(null); // []
// Undefined case
extractObjectKeys(undefined); // []
// Non-object values
extractObjectKeys('string'); // []
extractObjectKeys(123); // []
Notes
- This function only returns top-level (own) enumerable properties
- Returns an empty array for empty objects, null, undefined, or non-object values
- Internally uses
Object.keys()
with proper TypeScript typing - Symbol-keyed properties are not included in the result
- Inherited properties are not included (only own properties)
- The return type is properly typed as
Array<keyof T>
for better TypeScript support - For nested objects, only the top-level keys are extracted
- When using the
tuple
parameter, the order is determined by TypeScript's type system and may not match runtime order
extractObjectKeysDeep
Recursively extracts all nested keys from an object with proper typing.
Import
import { extractObjectKeysDeep } from 'nhb-toolbox';
Function Signatures
extractObjectKeysDeep<T extends GenericObject>(obj: T): Array<DeepKeys<T>>;
Aliases
extractKeysDeep
- Alias forextractObjectKeysDeep
Usage
Basic Usage
const obj = {
a: 1,
b: {
c: 2,
d: {
e: 3
}
}
};
const keys = extractObjectKeysDeep(obj);
// Returns ['a', 'b', 'c', 'd', 'e']
With Empty Object
const keys = extractObjectKeysDeep({}); // Returns []
With Null/Undefined
const keys = extractObjectKeysDeep(null); // Returns []
const keys = extractObjectKeysDeep(undefined); // Returns []
API
Type Parameters
Parameter | Description |
---|---|
T | A generic object type |
Parameters
Parameter | Type | Description |
---|---|---|
obj | T | The object to extract keys from |
Return Value
Array<DeepKeys<T>>
: An array of all nested keys from the specified object, or empty array if the object is null/undefined/empty.
Examples
- Simple Nested Object
- Complex Object
- Edge Cases
const user = {
id: 1,
profile: {
name: 'John Doe',
address: {
street: '123 Main St',
city: 'New York'
}
}
};
const keys = extractObjectKeysDeep(user);
// ['id', 'profile', 'name', 'address', 'street', 'city']
const company = {
name: 'Tech Corp',
departments: {
engineering: {
team: 'dev',
lead: 'Alice'
},
sales: {
region: 'north',
manager: 'Bob'
}
}
};
const keys = extractObjectKeysDeep(company);
// ['name', 'departments', 'engineering', 'team', 'lead', 'sales', 'region', 'manager']
// Empty object
extractObjectKeysDeep({}); // []
// Null case
extractObjectKeysDeep(null); // []
// Undefined case
extractObjectKeysDeep(undefined); // []
// Flat object (same as extractObjectKeys)
extractObjectKeysDeep({ a: 1, b: 2 }); // ['a', 'b']
Notes
- Recursively extracts all nested keys from an object
- Returns an empty array for empty objects, null, undefined, or non-object values
- For only top-level keys, use
extractObjectKeys
or its aliasextractKeys
- The return type is properly typed as
Array<DeepKeys<T>>
for better TypeScript support - Useful when you need to access all properties in a deeply nested object structure