In this lesson you will
- Read and write generic functions with a type parameter such as T
- Use built-in generic types: Array<T>, Promise<T> and Map<K, V>
- Write generic types such as Result<T> and Page<T> for data wrappers
- Limit a type parameter with extends and keyof
The problem generics solve
Imagine a helper that returns the first item of any list. If you type it as function first(list: any[]): any, it compiles, but every result is any, so TypeScript stops checking whatever you do next. Call first(["Deira", "Al Barsha"]) and then .toFixed(2) on the result, and nobody warns you that strings have no toFixed. Typing it as number[] fixes that for numbers but now it refuses a list of names. You want one function that remembers the type of what went in.
That is a type parameter. Write function first<T>(list: T[]): T | undefined. The <T> after the name declares a placeholder, and the rest of the signature uses it: the list holds T values and the function returns a T, or undefined for an empty list. You almost never write T yourself when calling: first([45, 12.5]) makes TypeScript infer that T is number, and first(["Deira"]) makes it string. The name T is only a convention; longer names such as Item are fine and often clearer.
Console
- Console messages, errors, form data and clicked links appear here.
Line 19 has the same mistake twice: calling a number method on a shop name. TypeScript reports it only for b, which came from the generic function. For a it says nothing, because any switched checking off. At run time both are undefined, so the generic caught a real bug that any let through.
Sign in free to read the full lesson
You are reading the free preview. Create a free account (it takes one minute, no password) to unlock the rest of this lesson and track your progress.
Unlocks after sign-in:
- 3 more sections with live code examples
- Common mistakes to avoid
- Hands-on coding exercise with automatic checks
- Practice quiz and the graded lesson test
Free forever. No card, no paywall.
Questions people ask
What are generics in TypeScript in simple words?
A generic is a type with a blank to fill in. Array<T> is "an array of something", and Array<number> fills the blank with number. In functions, the blank is filled automatically from the arguments, so one function can work with many types and still keep each one exact.
Why is the type parameter usually called T?
T is short for "type" and is only a convention, borrowed from Java and C#. Other common names are K and V for keys and values. Any name works, and a descriptive one such as Item or Row is often easier to read.
What is the difference between a generic and the any type?
Both accept values of any type, but a generic remembers which type it got and keeps checking with it, while any turns checking off. With last<T>, a list of strings gives back a string; with last(list: any[]): any, it gives back any and every later mistake goes unnoticed.
What does K extends keyof T mean?
It declares a second type parameter K that must be one of the property names of T. It is used for functions that take an object and the name of one of its properties, so a misspelt name is a compile error and the result has the right type, written T[K].
Do generics make TypeScript code slower?
No. Like all types, generics are removed when the code is compiled, so the JavaScript that runs is the same with or without them. Very complex generic types can slow down the type checker in your editor, which is another reason to keep them simple.
Sources
Facts in this lesson were checked against these official pages on 17 November 2026.
- TypeScript Handbook: Generics
- TypeScript Handbook: Keyof Type Operator
- TypeScript Handbook: Indexed Access Types
- TypeScript Handbook: More on Functions (generic functions and guidelines)
Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.