Generics
Learn how to create reusable components using generics.
1
Generics
Welcome to the next lesson! In this lesson, you’ll learn about generics, a powerful feature in TypeScript that allows you to create reusable and type-safe components. Let’s get started!

2
What Are Generics?
Generics let you write reusable code that works with any type. They act as placeholders for types that you can specify when you use them.
Example: A generic function:
Task: Write a generic function that takes an argument and returns it unchanged.
function identity<T>(value: T): T {
return value;
}
console.log(identity<string>("Hello"));
// Output: Hello
console.log(identity<number>(42));
// Output: 42