Q13. What is an indexed access type?
An indexed access type T[K] obtains the type of property K on T. It is useful in generic property accessors and type transformations because the selected key determines the output type.
For type User = { name: string; age: number }, User["age"] is number.
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 is `({ name: string; age: number })["name"]`?