keyof
Produces a union of the property key names of a given type.
Syntax
keyof Type Examples
interface User { id: number; name: string }
type UserKeys = keyof User; // 'id' | 'name'
function get<T, K extends keyof T>(obj: T, key: K): T[K] {
return obj[key];
}
get({ a: 1, b: 'x' }, 'b'); // string
Notes
The `keyof` operator yields the literal union of an object type's keys. For an
index signature `{ [k: string]: V }` it yields `string | number`. Commonly
paired with indexed access types and generic constraints for type-safe
property access.