0Pricing
TypeScript Academy · Lesson

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!

Generics — illustration 1

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

All lessons in this course

  1. Generics
  2. Type Aliases and Interfaces
  3. Utility Types
  4. Type Guards
← Back to TypeScript Academy