Skip to main content

ObjectKeys

Can be used to clearly define the return type of the Object.keys function. Keys of type symbol are excluded.


Interface

typescript
type ObjectKeys<T extends Record<PropertyKey, any>> = Exclude<
keyof T,
symbol
>;

Usage

Type Extraction Case

typescript
import { ObjectKeys } from '@modern-kit/types';

type MyObject = { a: string; b: number };
type MyKeys = ObjectKeys<MyObject>; // 'a' | 'b'

Object.keys Type Assertion Case

typescript
import { ObjectKeys } from '@modern-kit/types';

const obj = { a: 1, b: 2, c: 3 } as const;
const keys = Object.keys(obj) as ObjectKeys<typeof obj>[]; // ('a' | 'b' | 'c')[]