Q14. Why use generics?

Generics allow reusable functions and types to preserve relationships between input and output types. They are more precise than any when the same type parameter links several positions, and inference often chooses the type argument automatically.

An identity function using <T>(value: T): T returns the same static type it receives.

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 the main benefit of `identity<T>(value: T): T`?