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
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.