Generics are type placeholders.
Direct answer
Generics let you write one function or type that works for many types without losing type safety. You declare a type parameter, often T, and TypeScript fills it in when you use it.
A generic function
function first<T>(items: T[]): T {
return items[0];
}
const n = first([1, 2, 3]); // number
const s = first(["a", "b"]); // string
Generic interface
interface ApiResponse<T> {
data: T;
error: string | null;
}
const res: ApiResponse<{ name: string }> = {
data: { name: "Ada" },
error: null,
};
Constraints
function getLength<T extends { length: number }>(x: T) {
return x.length;
}
Tips
- Start with plain types. Add generics when you see repetition.
- Use clear names like
TItemwhenTis not enough.
Where Teyro fits
Teyro's TypeScript for Beginners course turns this into short lessons you can play in a few minutes a day. It has 20 lessons, and you can start free.
The bottom line
Generics keep code flexible and safe. They look scary but are simple placeholders.


