keyof
The TypeScript type operator that produces a union of an object type's property names, such as "id" | "table" | "rowCount".
In more detail
keyof is the basis of type-safe property access: K extends keyof T lets a function accept only real keys of T, so typos fail to compile and editors suggest valid names. Keys can be strings, numbers or symbols.
Related terms
- indexed access type — A type written T[K] that looks up the type of property K on type T, such as UserEntity["email"] giving string.
- generic constraint — A rule on a type parameter that limits which types may fill it, such as requiring a class, a particular interface or a certain shape.
- mapped type — A type that builds a new object type by looping over a set of keys, such as { [K in keyof T]: T[K] }.
Taught in
- Generic Interfaces, Classes And Type Aliases — Production TypeScript
- Generic Constraints: extends, keyof And Indexed Access — Production TypeScript
- Const Type Parameters: Retaining Literal Types In Functions — Production TypeScript
- Mapped Types, Key Remapping With as And Template Literals — Production TypeScript