Introduction to Generics

Learn about the concept of generic code in TypeScript to work with any sort of type, interface, or class definition.

We'll cover the following

Introduction

We have been exploring the type system within TypeScript and how it relates to interfaces, classes, and primitive types. We have also explored how to use various language features to mix and match these types, including type aliases and type guards.

All of the techniques we have used, however, eventually boil down to writing code that will work with a single particular type. While this approach can be effective for ensuring type safety, it can also be limiting, especially when we want to write code that works with any sort of type, interface, or class definition.

Need for generics

What if we would like to write some code that will work with any sort of type or any sort of interface or class definition? Perhaps a function that needs to find an element in a list, where the list could be made of strings, numbers, or any other type. This is where generics come into play.

Generics provide a mechanism to write code that does not need to specify a specific type. It is left up to the caller of these generic functions or classes to specify the type that the generic will be working with.

There are, of course, some constraints that come into play when working with generics.

  • How do we limit the types that generic code can work with down to a small subset of classes or interfaces?
  • Are we able to specify multiple types within generic code, and can we specify a relationship between these two generic types?

Get hands-on with 1200+ tech skills courses.