Write array helpers that preserve element types.
Section: Generics
Generic array helper
typescript
typescript
function first<T>(items: T[]): T | undefined {
return items[0];
}Explanation
A generic function can infer `T` from the array argument.
Learn the surrounding workflow
Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.
Related commands
Same sheet · prioritizing Generics
Generic identity function
Use a type parameter for reusable function typing.
Constrain generic types
Require a minimum shape for a type parameter.
Constrain a key parameter with `keyof`
Accept only valid property names for a given object type.