-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating

The TypeScript Workshop
By :

Intersection Types allow you to combine types to form a new type with the properties of the combined types. This is useful in cases where you have an existing type that does not, by itself, address some data you need to define, but it can do so in combination with another existing type. This is similar to multi-class inheritance, as the child object can have more than one parent object that it derives its properties from.
Let's say you have a type A
with a name and age property. You also have a type B
with a height and weight property. In your application, you find that there is a need for a person type: you want to track the user's name, age, height, and weight. You can intersect type A
and B
to form a Person
type. Why not just create a new type you ask? Well, this takes us back to wanting to be good coders and good coders stay DRY – Don't Repeat Yourself. Unless a type is truly unique in your application, you should reuse as much...