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[]): T

Example

Example
function first<T>(items: T[]): T | undefined {
  return items[0];
}
console.log(first([1, 2, 3]));

More in this group

as const Awaited<T> Generic constraint Indexed access keyof Parameters<T> Partial<T> Pick<T, K> / Omit<T, K>

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.

Open the course