Skip to main content
D
TypeScript
TypeScript Generics

TypeScript Generics

Generics act like variables for data types.

Read Time
5 min read
Difficulty
Beginner
Last Updated
Jun 15, 2026
Version
v1.0.0

Introduction

Sometimes developers write functions that should work with numbers, strings, or arrays, but they still want strict safety. Generics allow you to pass a type into a function as an argument, just like you pass normal data into a function.

Example

Example
1/* this uses a generic type */
2function identity<T>(arg: T): T {
3  return arg;
4}
5
6let output1 = identity<string>("myString");
7let output2 = identity<number>(100);

Key Points

  • Generics use angle brackets like ``.
  • The letter `T` stands for "Type".
  • They make functions highly reusable without using the dangerous `any` type.
  • TypeScript can usually infer the generic type automatically.

Up Next

Continue your journey with the next topic.

Go to TS Enums