Menu

Generics

Module 7: Generics

Introduction

Some code works the same way for many types. A function that returns the first item of a list should work for products, orders, and customers, and should return the right type each time. Without generics, you either write the function many times or fall back to any and lose type safety.

Generics let you write code once with a type parameter, a placeholder that is filled in when the code is used. This module covers generic functions, types, and classes, how to constrain type parameters, defaults and const type parameters, and the generic patterns you will meet in real libraries.

What you will learn in this module

  • Why generics exist and how to write generic functions
  • How to create generic interfaces, type aliases, and classes
  • How to constrain type parameters with extends and keyof
  • How default type parameters, const type parameters, and NoInfer work
  • How to apply generics in real patterns, and when not to use them

TypeScript Tutorial

J

Jebasta





Get in Touch For Details