Generics — TypeScript · Foundations

Write code once for every type it can hold, without giving up the checks: generic functions whose output type follows their input, keyof and indexed access types, and a generic repository constrained to things with an id, the shape of every repository in NestJS.

What you will learn

Read the theory for Generics

All TypeScript lessons

All Foundations courses

loading types…

What you'll learn

  • Write generic functions whose return type follows from their arguments
  • Constrain a type parameter with extends, and use keyof and T[K] to type a key and its value
  • Write a generic class and use it for different types