# Union Types and Intersection Types

Do you now about union and intersection types? They are widely used but rarely named. The only programming language I know about containing these features and daring to call them this way is Typescript.

## A simple definition of Union and Intersection Types

In Typescript, union and intersection are defined in the Advanced Types section.

An intersection type combines multiple types into one.

A union type describes a value that can be one of several types.

Usage and notation are quite simple, the `&`

symbol is used to build an intersection while the `|`

symbol is for union.

If you have some history in programming, you may notice these symbols are usually linked to logical expressions. This is important, as an intersection can be read as a `And`

operator and a union as a `Or`

.

The return type of `getSmallPet`

can be read as either a `Fish`

**or** a`Bird`

. On the other hand, `getFlyingFish`

return type can be read as a Fish **and** a Bird.

Now that all this looks safe and clear, let’s ask uncomfortable questions.

What functions can I call on the returned values? If a `FlyingFish`

is a `Fish`

**and** a `Bird`

it means I can call functions of both, right? But it’s an **intersection**, it should only contain what’s in both, which means nothing in this case. And `getSmallPet`

returned value, do you think I can call functions on it? It’s a **union** so it should contain all the values from both parts right?

## Union and intersection in set theory

The definition most of us implicitly use when thinking about unions and intersections came from Set theory.

Union of the sets

AandB, denotedA∪B, is the set of all objects that are a member ofA, orB, or both. The union of {1, 2, 3} and {2, 3, 4} is the set {1, 2, 3, 4} .Intersection of the sets

AandB, denotedA∩B, is the set of all objects that are members of bothAandB. The intersection of {1, 2, 3} and {2, 3, 4} is the set {2, 3} .

Using the above definitions, the intersection of `Bird`

and `Fish`

should be all the elements in common.

Bird ∩ Fish = {}

The union of `Bird`

and `Fish`

should be all the elements from `Bird`

and all the elements from `Fish`

.

Bird ∪ Fish = { fly, swim }

In programming term, does this mean a `FlyingFish`

can neither `swim`

nor `fly`

?

## Where is the error?

Typescript and Set Theory seems to be the exact opposite. How can this be?

It’s not. Because I lied to you (*sorry*). Until now, I voluntarily made the usual mistake : thinking values of a type are its properties. In fact, they are not!

Things are clearer with primitive types : A type values are its instances. `Char`

hold no properties, but still, it represents an interval of values.

Char ∩ UnsignedChar <=> [-128, 127] ∩ [0, 255] <=> [0, 127]

So in a matter of fact, the `PositiveChar`

intersection corresponds to the set intersection.

Union is just the same :

The `Primitive`

type can hold the same values that `string`

and `int`

combine.

## Conclusion

Intersection and union types syntax gives a pretty good hint on what they are meant for. But when thinking a bit more deeply they may seem strange.

The usual error is thinking values of a type are its properties. Actual type’s values are its instances. Therefore, intersection holds fewer values than it’s component and can be applied to any function meant for them. Equally, union creates a new, more open, less strict type. You can’t be sure the functions bound still hold for this new type.

With this in mind :

— Union types can hold any instance of its components but can’t use functions of one. It can only use properties defined in all its components.

— Intersection types can hold a subset of its components instances but can use functions of any of them.