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
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
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
Summary
| Type | Examples | Description |
|---|---|---|
| Generic | <T> | Placeholder for ANY type |
| Constraint | <T extends X> | Limit which types are allowed |
| Class | class Box<T> | Class that works with any type |
| Method | function f<T>() | Run logic on any type |
| Best For | Reusable logic, collections, type-safe APIs | |
| Infer | Often you don't even have to write types! (Type Inference!) | |
| Safe | Impossible to use the "wrong" data type with a generic! | |
| Clean | Your code reads like logical documentation | |
| Generic T. | Use T, U, V for many parameters! | |
| Standard | Established and trusted worldwide for 10+ years | |
| React | Standard way to write Props and State in React + TS | |
| Library | Most JS libraries provide generics for their data! | |
| Constraint | Use extends to "contract" your generic requirements! | |
| Default | <T = string> (Set a default type!) | |
| Multiple | <T, U> (Combine two or more types!) | |
| Logic | Code once; use for many different types! | |
| Identity | An object never "forgets" its real type internally | |
| Refactor | Rename a property, and it updates across all instances! | |
| Tooling | Perfect autocomplete for generic calls in VS Code | |
| Interface | You can also describe a generic with an interface! | |
| Array | Array<T> is a built-in generic type! | |
| Promise | Promise<T> (Describe what a task will return!) | |
| Search | find<T>() (Search logic that returns the correct type!) | |
| KeyOf | <T, K extends keyof T> (Accessing object keys safely!) |