Intersection Types#

Intersection Types Express "and"#

Intersection types allow us to express "AND" relationship between types. They are relevant for object types, but not for primitives as a value cannot be multiple primitives at the same time.

Intersections can be used to extend object types, allowing us to create new types that build off of existing ones. In the example below, we use the intersection operator, &, to create the FoodProduct type as an intersection of Product and Food:

The order of the types in an intersection expression is irrelevant and the resulting type will be the same regardless. This means we could have written FoodProduct like so:

FoodProduct is assignable to objects that have properties from both Food and Product:

 

This page is a preview of Beginners Guide to TypeScript

No discussions yet