TypeScript · Generics and utility types
Generic function
A type parameter makes a function work with many types while keeping the relationship between inputs and output.
Syntax
function first<T>(items: T[]): TExample
Example
function first<T>(items: T[]): T | undefined {
return items[0];
}
console.log(first([1, 2, 3]));More in this group
Learn this in the course
Reference pages are the quick answer. The course explains the why, shows the full example and ends with a challenge.