TypeScript Tutorial

Generics

Type-safe functions and classes with <T> and constraints

What are TypeScript Generics?

In TypeScript, Generics are a way to write generic, reusable code that works correctly with any data type, while still maintaining high-level Type Safety. This is essential for building flexible libraries, state managers, and data collections (like a List or a Result).
typescript
// Generic Type Name: <T> (Standard!)
function identity<T>(value: T): T {
  return value;
}

// Any type can work!
let num = identity<number>(10);
let str = identity<string>("Alpha");

How It Works: Type Parameters (<T>)

You can create your own "generic" functions and classes using a placeholder type, usually called T (for Type).
typescript
class Box<T> {
  private content: T;

  constructor(content: T) {
    this.content = content;
  }

  get(): T { return this.content; }
}

// Inferred as Box<number>!
const intBox = new Box(100);

Comparisons: The Two Essential Points

Wait! TypeScript is smart. You rarely have to provide the <Type> manually.
  • Explicit: identity<string>("Hi")
  • Implicit: identity("Hi") (TS "infers" the type automatically!)

Generic Constraints (extends)

What if you want a method that only works with things that have a .length property? Use the extends keyword to safely "limit" what types are allowed.
typescript
interface HasLength {
  length: number;
}

// T MUST have a length property!
function logLength<T extends HasLength>(item: T): void {
  console.log(item.length);
}

logLength("Alpha"); // OK (String has length!)
logLength([1, 2]); // OK (Array has length!)
// logLength(123); // ERROR: number does NOT have length!

Summary

TypeExamplesDescription
Generic<T>Placeholder for ANY type
Constraint<T extends X>Limit which types are allowed
Classclass Box<T>Class that works with any type
Methodfunction f<T>()Run logic on any type
Best ForReusable logic, collections, type-safe APIs
InferOften you don't even have to write types! (Type Inference!)
SafeImpossible to use the "wrong" data type with a generic!
CleanYour code reads like logical documentation
Generic T.Use T, U, V for many parameters!
StandardEstablished and trusted worldwide for 10+ years
ReactStandard way to write Props and State in React + TS
LibraryMost JS libraries provide generics for their data!
ConstraintUse extends to "contract" your generic requirements!
Default<T = string> (Set a default type!)
Multiple<T, U> (Combine two or more types!)
LogicCode once; use for many different types!
IdentityAn object never "forgets" its real type internally
RefactorRename a property, and it updates across all instances!
ToolingPerfect autocomplete for generic calls in VS Code
InterfaceYou can also describe a generic with an interface!
ArrayArray<T> is a built-in generic type!
PromisePromise<T> (Describe what a task will return!)
Searchfind<T>() (Search logic that returns the correct type!)
KeyOf<T, K extends keyof T> (Accessing object keys safely!)