Generics in TypeScript
In Computer Science, generics refer to a programming language feature that allows for the creation of functions, classes, and data structures that can operate on multiple data types.
The concept of generics is rooted in the idea of parameterizing types or algorithms over other types. This means that you can define a function, class, or data structure with placeholders for the types it works with, rather than specifying concrete types.
1. Introduction to TypeScript Genericsβ
Discover how to use TypeScript generics to develop generic functions. Generics enable you to write functions that can work with a variety of data types, providing type safety and code reusability.
Example -
// Generic function to echo an argument
function echo<T>(arg: T): T {
return arg;
}
let result1: string = echo("Hello, TypeScript Generics!");
let result2: number = echo(42);
console.log(result1); // Output: Hello, TypeScript Generics!
console.log(result2); // Output: 42
2. Generic Constraintsβ
Generic constraints allow you to restrict the types that can be used with generics, providing additional type safety and enabling more precise type inference.
Example -
// Generic function to log the length of an array
function logLength<T extends { length: number }>(arr: T): void {
console.log(`Length of array: ${arr.length}`);
}
logLength([1, 2, 3]); // Output: Length of array: 3
logLength("typescript"); // Output: Length of array: 10
// logLength(42); // Error: Argument of type '42' is not assignable to parameter of type '{ length: number; }'
3. Generic Classesβ
Generic classes allow you to create classes that can work with different types of data, providing flexibility and code reusability.
Example -
// Generic class for a pair of values
class Pair<T, U> {
constructor(public first: T, public second: U) {}
toString(): string {
return `(${this.first}, ${this.second})`;
}
}
let pair1 = new Pair<number, string>(1, "one");
let pair2 = new Pair<string, boolean>("yes", true);
console.log(pair1.toString()); // Output: (1, one)
console.log(pair2.toString()); // Output: (yes, true)
4. Generic Interfacesβ
Generic interfaces enable you to define interfaces with generic type parameters, allowing for the creation of flexible and reusable abstractions.
Example -
interface Pair<T, U> {
first: T;
second: U;
}
let pair: Pair<number, string> = { first: 1, second: "two" };
console.log(pair); // Output: { first: 1, second: "two" }
These implementations demonstrate how to use TypeScript generics to create generic functions, add constraints to generic types, develop generic classes, and define generic interfaces, showcasing the flexibility and reusability provided by generics in TypeScript.