Generics in typescript
Generics create reusable components that work with multiple types while maintaining type safety. They're used in functions, classes, and interfaces. Constraints (extends) limit generic types. Built-in generics include Array<T>, Promise<T>, Record<K,V>.
Example
function first<T>(arr: T[]): T | undefined {
return arr[0];
}
const num = first([1, 2, 3]); // number
const str = first(['a', 'b']); // stringThe generic T is inferred from the argument, providing correct return types automatically.