You are viewing a free preview of this lesson.
Subscribe to unlock all 12 lessons in this course and every other course on LearningBro.
Generics allow you to write flexible, reusable components that work with a variety of types rather than a single one — without sacrificing type safety. They are one of TypeScript's most powerful features and are used extensively in libraries, frameworks, and everyday application code.
Without generics, you would have to choose between writing type-safe code that only works with one type, or using any and losing type safety entirely:
// Too restrictive — only works with numbers
function firstNumber(arr: number[]): number {
return arr[0];
}
// Too loose — no type safety
function firstAny(arr: any[]): any {
return arr[0];
}
// Just right — generic
function first<T>(arr: T[]): T {
return arr[0];
}
const name = first(['Alice', 'Bob']); // string
const num = first([1, 2, 3]); // number
Declare a type parameter in angle brackets before the parameter list:
function identity<T>(value: T): T {
return value;
}
// TypeScript infers T from the argument:
const str = identity('hello'); // string
const num = identity(42); // number
// Or specify explicitly:
const bool = identity<boolean>(true);
You can use multiple type parameters:
Subscribe to continue reading
Get full access to this lesson and all 12 lessons in this course.