Creating intersection types

In this lesson, we'll learn what an intersection type is and how to create one.

Understanding an intersection type #

Intersection types, like union types, combine existing types to form a new type. An intersection type will have all the members from the types it is based on. Note that intersection types don’t contain only common members from the types it is based on, as you may first expect.

An intersection type is constructed from existing types using the ampersand (&) character:

type A_and_B_and_C = A & B & C;

The example below combines Name and PhoneNumber types to create a Contact type containing firstName, lastName, landline, and mobile properties:

Get hands-on with 1200+ tech skills courses.