TypeScript Generics Explained Simply, with Examples
A generic is a function between types: pass a type in, get a type out. Generic functions, interfaces and the identity example, step by step.
In this chapter
Generics are functions between types
TypeScript generics: common questions
What are generics in TypeScript?
Type parameters, like <T>, that let one function or type work with many types while keeping the link between input and output, for example first<T>(items: T[]): T.
More on Generics
Keep learning
Overview
chapters · generics Generics are functions between types
1. The helper that forgets what you gave it
A tiny helper takes the first item from a list. Typed with any (the "switch off checking" type: anything goes), it throws the type away. any in, any out number | undefined Property 'toUpperCase' does not exist on type 'number'. TypeScript knows tip is a number (or undefined). Numbers have no toUpperCase, so it stops you before the code ever runs.
2. A type parameter is an input
the call list: T[] ← number[] match argument to parameter returns number | undefined How first(['pad thai', 'ramen']) gets its type argument type match T[] against string[] substitute T string | undefined
3. Two levels, same idea
Runs in the checker (the part of TypeScript that finds errors). Input is a type. All types are deleted from the JavaScript that actually runs. Order is an order type defined elsewhere. type expression evaluates to arrays are generic too await unwraps it to Receipt Generic type 'Stamped' requires 1 type argument(s). In plain words: Stamped has a blank, and you left it empty.
4. Inference fills the slot for you
Two words for the table below. A literal type is one exact value used as a type, like "pizza". Widening means TypeScript swaps it for the general type, like string.
identity: the smallest generic
you filled T yourself first(['a', 'b']) string | undefined first([1, 'two']) string | number | undefined Argument of type 'string' is not assignable to parameter of type 'number'. You set T to number, then passed a string. It doesn't fit.
5. Predict it
["sushi", 12] [string, number] (string | number)[] Non-generic helper One fixed type, or any. Callers lose what they passed in. Generic helper Output type is tied to input type. Callers keep full autocomplete.
Remember
<T> is a parameter, but for types. Each call fills T, usually by inference from arguments. Generic types (Stamped<T>) are functions that run in the checker. Generics tie the output type to the input type. ! Cheat code Filled by arguments, or <string> by hand → Deeper: constraints and defaults, how inference works, conditional types, generic React components.
More courses
- Jobs, linked to lessons — Browse jobs where every requirement links to the chapter that teaches it. See how ready you are, how long the gap takes to close, and which listings look stale.
- Match a job post to courses — Paste a job post. Each skill is matched to a track here and put in learning order, foundations first.
- React patterns from open source — Patterns extracted from Material UI, Radix, React Aria, React Hook Form, Redux Toolkit, TanStack, and Next.js — not slides about hooks.
- Databases in depth — How a query becomes work. Indexes, isolation, locks, and the plans that explain why a page is slow — not an ORM tour.
- System Design — Core concepts first (SQL vs NoSQL, CAP, indexing, replication, sharding), then designs grouped by what carries them: the database, Redis, Kafka, or many services.
- NestJS: build a real API — One ticket-booking API, built in order: a plain Node server first, then NestJS modules, validation, a real database, and the features real clients lean on.
- Microservices — Microservice patterns one diagram at a time: how to split a monolith, how services talk, and how data stays consistent without distributed transactions.
- Kafka — A beginner-to-senior path through event logs, reliable delivery, production operations, and disaster recovery.
- AI engineering for software engineers — Eight free reads, in order, for software engineers moving into AI: when to build an agent, how to build one, how to test it, and how the model works underneath.
- Frontend system design — How a UI is sliced when it is a product, not a page: ownership of state, cache, routes, and what happens when the network lies.
- CSS in depth — See the box, then the page. Visual chapters from the box model through Flexbox surprises, Grid, selectors, and units, then backgrounds, shapes, and visual effects, each with a playground.
- Accessibility — Foundations first (WCAG in plain words, semantic HTML, lists, alt text), then hands-on HTML labs, a full pass of WAI-ARIA 1.3, and a final fix-the-page challenge.
- Docker — What an image is, why layers cache, and how containers find each other on a network.
- Redis — Fifteen Redis patterns, one picture each: cache, session, lock, counter, rate limiter, queue, and when Redis is the wrong tool.
- Kubernetes — Kubernetes from zero to operators: why it exists, Pods, Deployments and Services, then configuration, networking, storage, scheduling, scaling and security.
- AWS — AWS networking in plain English. A VPC is a gated housing society: streets are subnets, the Internet Gateway is the main gate, and a route table is the signboard.
- Networking: the NetRoute game — NetRoute is a packet logistics game. You wire up computers, switches, routers, firewalls and gateways, set each device, and watch glowing packets travel. If a packet gets dropped, the game shows you which header field was wrong.