Q12. What is keyof?
keyof T produces a union of the known property keys of type T. It is often combined with generic constraints to restrict a parameter to valid keys and with indexed access T[K] to preserve the corresponding property value type.
Constraining K extends keyof T rejects misspelled keys and lets the return type depend on the selected property.
Note
Interview approach: define the concept, explain why it matters, then mention one trade-off or limitation.
TypeScript concept
// Illustrative TypeScript contract
type Identifier = string;
const id: Identifier = "item-1";💡 The example demonstrates the compile-time contract discussed in this question.
Key Takeaways
- Explain the concept in your own words.
- Know the practical trade-off and its limits.
- Use a small example to demonstrate the idea.
Knowledge Check
What does `keyof { id: string; active: boolean }` produce?