Home

Blog

TypeScript Generics, Demystified

TypeScript Generics, Demystified

Types that take arguments

A generic like Array<T> is a type that takes another type as input. You describe the relationship once and TypeScript fills in the specifics at each call site:

function first<T>(items: T[]): T | undefined {
  return items[0];
}

Constraints (<T extends { id: string }>) let you require capabilities while staying generic.

More posts