Lesson overview · Free interview practice

Generics

Master generic types and constraints.

Topics in the full lesson
  • Introduction to Generics
  • Generic Functions
  • Generic Classes
  • Using Constraints with extends
  • Keyof Constraint
  • Generic Interfaces
  • Generic Constraints and Interfaces
  • Best Practices
  • Exercises

Try the free interview questions

Work through a question before revealing its explanation. These questions and answers are free; Premium adds the full lesson walkthrough, examples and implementation detail.

TheoryEasy

Generics: Type Parameters and Why They Exist

Question

What is a generic type in TypeScript? Show the canonical identity example and explain why generics beat any for reuse.

Take a moment to think about this before revealing the answer

Explain your reasoning or try an implementation before comparing answers.

TheoryEasy

Generics vs `any`: Preserving Type Information

Question

When you have a function that "works with any type," why prefer a generic over any? Show the difference in inferred return types.

Take a moment to think about this before revealing the answer

Explain your reasoning or try an implementation before comparing answers.

TheoryMedium

Generic Constraints with `extends`

Question

What is a generic constraint, and why do you need one? Show a generic function that accesses properties of its parameter — with and without a constraint.

Take a moment to think about this before revealing the answer

Explain your reasoning or try an implementation before comparing answers.

TheoryMedium

Generic Classes

Question

How do you define a generic class? Show the canonical container example (Stack<T>) and explain how constraints flow to methods.

Take a moment to think about this before revealing the answer

Explain your reasoning or try an implementation before comparing answers.

TheoryMedium

`keyof` and Indexed Access Types

Question

What does the keyof operator do, and what is an indexed access type? Show how they combine with generics to make property access type-safe.

Take a moment to think about this before revealing the answer

Explain your reasoning or try an implementation before comparing answers.

Explore the full Generics material

Premium includes the complete lessons and implementation references. Free practice questions remain available without a subscription.

All course tracks & premium content
From basics to advanced masterclasses
Built for JS/TS developers like you
Real-world tips & common pitfalls
Upgrade to Premium

Continue in this section