What is the keyof Operator in TypeScript?
Learn the TypeScript keyof operator: build a union of a type's keys, pair it with T[K] and generics for type-safe property access, with a clear example.
Expected Interview Answer
The keyof operator produces a union of the literal string (or number/symbol) keys of a given object type, giving you a type that represents all valid property names of that type.
keyof is a type-level operator: keyof T returns 'a' | 'b' | 'c' for a type T with those keys. It is most powerful combined with generics and indexed access types (T[K]), letting you write functions that accept only real property names and return the exactly-typed value at that key. This underpins type-safe property getters, and it stays in sync automatically when the source type changes.
- Restricts arguments to real property names
- Enables type-safe property access with T[K]
- Stays in sync as the source type changes
- Powers mapped and utility types
- Catches typos in key strings at compile time
AI Mentor Explanation
keyof is like the fixed set of labelled columns on a scorecard: runs, wickets, overs, extras. You can only ask for a stat that actually has a column heading, so requesting 'runs' works but 'goals' is rejected outright because no such column exists on a cricket scorecard.
Step-by-Step Explanation
Step 1
Start with an object type
Define an interface or type whose keys you want to reference, such as { id: number; name: string }.
Step 2
Apply keyof
keyof T yields a union of that type's key literals, e.g. 'id' | 'name'.
Step 3
Constrain a generic
Use K extends keyof T so a parameter can only be a real key of T.
Step 4
Index into the type
Use T[K] to get the exact type of the value stored at that key.
Step 5
Get type-safe access
A getter typed (obj: T, key: K) => T[K] returns the precise value type and rejects invalid keys.
What Interviewer Expects
- That keyof yields a union of key literal types
- The pairing of keyof with indexed access types T[K]
- How K extends keyof T constrains a generic parameter
- That keyof stays in sync when the source type changes
- A correct type-safe property getter example
Common Mistakes
- Confusing keyof (type level) with Object.keys (runtime)
- Expecting keyof to return values instead of key names
- Forgetting keyof also includes number and symbol keys
- Not constraining with K extends keyof T, allowing invalid keys
Best Answer (HR Friendly)
“keyof is a TypeScript operator that gives you the set of valid property names of a type. It lets you write functions that only accept real keys and return the correctly typed value, so misspelled or nonexistent property names are caught while you code.”
Code Example
interface User {
id: number;
name: string;
active: boolean;
}
type UserKeys = keyof User; // 'id' | 'name' | 'active'
function getProp<T, K extends keyof T>(obj: T, key: K): T[K] {
return obj[key];
}
const user: User = { id: 1, name: 'Ada', active: true };
const name = getProp(user, 'name'); // name: string
const active = getProp(user, 'active'); // active: boolean
// getProp(user, 'email'); // Error: 'email' is not a key of UserFollow-up Questions
- How does keyof differ from Object.keys at runtime?
- What is an indexed access type and how does T[K] work?
- How do keyof and mapped types work together?
- What does keyof return for an index-signature type?
- How does keyof handle number and symbol keys?
MCQ Practice
1. What does keyof T produce?
keyof T yields a union of the literal key names of T, e.g. 'id' | 'name'.
2. Which constraint restricts K to real keys of T?
K extends keyof T limits K to the union of T's actual property names.
3. What does T[K] represent when K extends keyof T?
T[K] is an indexed access type giving the exact type of the value stored at key K.
Flash Cards
What does keyof return? — A union of the literal key names of a type, e.g. keyof {a:1;b:2} is 'a' | 'b'.
keyof vs Object.keys? — keyof is a compile-time type operator; Object.keys is a runtime call returning a string array.
What is T[K]? — An indexed access type giving the value type at key K, used with K extends keyof T.
Why constrain with keyof? — K extends keyof T rejects invalid keys at compile time and enables precise T[K] returns.