Extending Types in Generics

Learn how to extend generic types and limit it to some values.

Extending generics types

Let’s say we wanted to limit the value of our linked list node to only support strings, numbers, and booleans. In TypeScript, constraints are expressed using the extends keyword. T extends K means it’s safe to presume that a value of type T is also of type K. An easy way of thinking about this is to just imagine we’re writing something with extends as a variable assignment.

Let’s take 19 extends number as an example. We could think about it like this:

Get hands-on with 1400+ tech skills courses.