objectKeys
a function that works the same as Object.keys()
, but protects the key type.
Note that the symbol property is excluded because it is not an enumeration.
Interface
type ObjectKeys<T extends Record<PropertyKey, T[keyof T]>> = Exclude<
keyof T,
symbol
>;
const objectKeys: <T extends Record<ObjectKeys<T>, T[keyof T]>>(
obj: T
) => ObjectKeys<T>[];
Usage
import { objectKeys } from '@devgrace/utils';
const symbol = Symbol('d');
const obj = {
a: 1,
b: 2,
c: 3,
[symbol]: 4,
} as const;
/**
* type: ("a" | "b" | "c")[]
* value: ["a", "b", "c"]
*/
const keys = objectKeys(obj);