Explain Generics in TypeScript and write a generic utility function.
expand_more
function getFirstElement<T>(arr: T[]): T | undefined {
return arr[0];
}
This ensures that if you pass an array of numbers, the return type is checked as a number, and if you pass strings, it is treated as a string.