type alias KeyOf

This is a utility type that allows you to get the keys from an object or list.

If T is a type of list, then this behaves the same as ValuesOf.

Examples

Example 1

const MyEnum = { foo: 'bar', another: 'baz' } as const;
const MyList = ['apple', 'banana', 'orange'] as const;

function example(param: KeyOf<typeof MyEnum>): void {
  // param would be: "foo" | "another"
}

function example2(param: KeyOf<typeof MyList): void {
  // param would be: "apple" | "banana" | "orange"
}

Type Parameters

Definition

T extends Readonly<unknown[]> ? ValueOf<T> : keyof T

Usage

import { type KeyOf } from ".";