TypeScript Advanced Types and Guards

Intersection

An intersection type allows you to combine multiple types. Let’s consider a college course.

Union

Union types, on the other hand, say an object can be one thing OR another. For a simple example:

Type Guards

Where things become interesting is when you combine these two types. Let’s say you had a student, where you did not know if they were a TA or not.

Discriminated Unions

Another guard comes about when dealing with discriminated unions. These union types have a field that is always declared as a literal. This literal allows TypeScript to distinguish between types.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store