Skip to content

TypeScript Generics for Beginners: Type Parameters, Generic Types, extends and keyof

NeedMyTools Academy

Sign in free to read the full lesson

Sign in or create a free account

Free forever. No card, no paywall.

Some code does not care what kind of value it handles. A function that returns the last item of a list works the same for prices, product names or branch objects. A response from a server always has the same wrapper, whatever data sits inside. You could type these with any, but then TypeScript forgets everything the moment the value goes in, and the bugs come back. Generics solve this: you give the type a name, usually T, and TypeScript fills it in each time the code is used. You have already used generics without noticing, because number[] is short for Array<number>. This lesson shows how to read them and how to write your own, without turning simple code into puzzles.

Quick answer

A generic adds a type parameter in angle brackets: function last<T>(list: T[]): T | undefined. When you call last([45, 30]), TypeScript infers that T is number and the result is number | undefined. Types can be generic too: type Result<T> = { ok: true; data: T } | { ok: false; error: string }, used as Result<Product>. Limit what T may be with extends, as in <T extends { price: number }>, and use K extends keyof T to accept only real property names of T. Use a generic when the same type appears in two places, such as the input and the output; otherwise a plain type is simpler.

35 min read and practice · Content checked on 17 November 2026

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.

Try it: any forgets the type, a generic keeps it
Code
ResultUpdates as you type
Console
  1. 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
Sign in or create a free account

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.

Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.