What is a Generic in TypeScript?
Understand TypeScript generics with clear examples: reusable type-safe functions, generic constraints with extends, and how generics are erased at compile time.
Expected Interview Answer
A generic is a way to write a function, class, or type that works with a placeholder type parameter instead of a fixed type, so the caller decides the concrete type while TypeScript still checks it for consistency.
Instead of writing separate functions for `number[]` and `string[]`, you write `function first<T>(arr: T[]): T` once, and TypeScript infers `T` from whatever is passed in. This preserves type information across the function boundary — the return type is tied to the input type rather than widened to `any`. Generics can be constrained with `extends` to require a minimum shape, and given default type parameters. They exist purely at compile time and are erased from the emitted JavaScript, unlike runtime generics in languages such as Java.
- Reuse one implementation across many types safely
- Preserves specific type information instead of falling back to `any`
- Constraints (`extends`) let you require a minimum shape on `T`
- Catches misuse at compile time rather than at runtime
- Powers strongly typed reusable utilities like arrays, promises, and React hooks
AI Mentor Explanation
A generic is like a training drill designed to work with whatever bat a player brings, rather than a drill built only for a specific willow. The coach specifies `<T extends CricketBat>` in effect, so the drill still checks the bat is legal equipment, but the actual bat used is decided fresh by whichever batter walks in.
Step-by-Step Explanation
Step 1
Declare a type parameter
Write `<T>` after the function or class name to introduce a placeholder type used throughout the signature.
Step 2
Use T in the signature
Reference `T` in parameter and return types, e.g. `function identity<T>(value: T): T`, so the return type tracks the input.
Step 3
Let inference work
Calling `identity(42)` infers `T` as `number` automatically; you rarely need to write `identity<number>(42)` explicitly.
Step 4
Constrain with extends
`function longest<T extends { length: number }>(a: T, b: T)` requires `T` to have a `length` property before it compiles.
Step 5
Erase at compile time
Generics exist only in the type system; the emitted JavaScript has no trace of `<T>` at all.
What Interviewer Expects
- Can write a basic generic function signature from scratch
- Explains that generics preserve type information instead of widening to `any`
- Knows how to constrain a type parameter with `extends`
- Understands generics are erased and have zero runtime presence
- Can give a real use case like a typed array utility or API wrapper
Common Mistakes
- Confusing generics with `any`, losing all type safety
- Forgetting to constrain `T` when a property is accessed on it
- Thinking generics exist at runtime and can be inspected there
- Overusing generics where a simple union type would suffice
Best Answer (HR Friendly)
“A generic lets you write one reusable piece of code that works safely with many different data types, instead of duplicating the same function for each type. TypeScript still checks that everything fits together correctly, which prevents bugs while keeping the code flexible.”
Code Example
function firstItem<T>(arr: T[]): T {
return arr[0];
}
const n = firstItem([1, 2, 3]); // n: number
const s = firstItem(['a', 'b']); // s: stringfunction longest<T extends { length: number }>(a: T, b: T): T {
return a.length >= b.length ? a : b;
}
longest('hi', 'hello'); // ok, strings have length
// longest(3, 4); // Error: number does not satisfy constraint { length: number }Follow-up Questions
- How do generic constraints with `extends` differ from a plain union type?
- What is a default type parameter and when would you use one?
- How does TypeScript infer generics from function arguments?
- Can you write a generic class, not just a generic function?
- What is the difference between generics in TypeScript and generics in Java at runtime?
MCQ Practice
1. What is the main purpose of a generic type parameter like `<T>`?
Generics let one implementation serve many types while TypeScript still tracks and checks the specific type used at each call site.
2. What does `function longest<T extends { length: number }>` require of T?
The `extends` constraint requires whatever type is passed for T to include a `length` property, not any specific concrete type.
3. What happens to generic type parameters in the emitted JavaScript?
Like all TypeScript types, generics are compile-time-only and are completely erased from the output JavaScript.
Flash Cards
What does `<T>` represent in a function signature? — A placeholder type parameter that the caller's argument type fills in, preserved through the return type.
How do you constrain a generic to require a shape? — Use `extends`, e.g. `<T extends { length: number }>`.
Do generics exist at runtime? — No — they are erased entirely at compile time, unlike Java's reified generics in some cases.
Why prefer a generic over `any`? — A generic preserves the actual input type through to the output, while `any` discards all type safety.