The keyof Type Operator
Get a union of an object type's keys with keyof.
The Keys of a Type
The keyof operator takes an object type and produces a union of its property keys. It is the foundation for type-safe property access and generic key utilities.
Basic keyof
Apply keyof to an interface to get a union of its key names as string literal types.
interface User {
id: number
name: string
}
type UserKeys = keyof User // "id" | "name"
const k: UserKeys = "name"
console.log(k)All lessons in this course
- The typeof Type Operator
- The keyof Type Operator
- Indexed Access Types
- Combining typeof and keyof