← Back to TypeScript Course | Chapter 7: Generics | Lesson 1 of 8

Introduction to Generics

Generics let you write reusable TypeScript code that works with different types while keeping type safety. Instead of choosing one fixed type, you define a type parameter that can be supplied when the code is used.

Why Generics Are Useful

Generics let one function or class work with many different types without losing information about the specific type actually passed in, unlike using any, which throws that type information away entirely.

Example: Why Generics Are Useful

typescript
function identity<T>(value: T): T {
  return value; // keeps the exact input type, unlike "any"
}
console.log(identity<string>("hello"), identity<number>(42));

Type Parameters

A type parameter such as T is a placeholder for a concrete type supplied when the generic code is actually used, similar to how a function parameter is a placeholder for a concrete value supplied at call time.

Example: Type Parameters

typescript
function wrapInArray<T>(value: T): T[] {
  return [value];
}
console.log(wrapInArray(5), wrapInArray("hi"));

Type Inference

TypeScript often infers a generic type from the supplied argument automatically, so explicit type arguments are only needed when the compiler genuinely can't figure out the intended type on its own.

Example: Type Inference

typescript
function identity<T>(value: T): T {
  return value;
}
console.log(identity(42)); // T inferred as number, no explicit argument needed

Generic Arrays

Generics are commonly combined with arrays to describe the element type while keeping the function reusable across arrays of numbers, strings, or any other type, all through the same generic implementation.

Example: Generic Arrays

typescript
function firstItem<T>(items: T[]): T {
  return items[0];
}
console.log(firstItem([1, 2, 3]), firstItem(["a", "b"]));

Generic Type Aliases

Type aliases can also have type parameters, making an object shape reusable with different data types plugged in — for example, a generic ApiResponse<T> shape that works whether T is a User, a Product, or anything else.

Example: Generic Type Aliases

typescript
type ApiResponse<T> = {
  data: T;
  status: number;
};
const response: ApiResponse<string> = { data: "OK", status: 200 };
console.log(response);
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.