TypeScript · Generics and utility types

Generic constraint

Restricts a type parameter so the function can rely on certain members.

Syntax
<T extends { id: string }>

Example

Example
function ids<T extends { id: string }>(items: T[]): string[] {
  return items.map(item => item.id);
}
console.log(ids([{ id: "a" }, { id: "b" }]));

More in this group

as const Awaited<T> Generic function 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