Q15. What is a generic constraint?
A constraint such as T extends { id: string } restricts a generic type argument to values that satisfy a required shape while retaining any additional information about T. It lets generic implementations safely access the constrained members.
The constraint promises an id property but does not require T to contain only id.
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 `T extends { id: string }` permit?