Discriminating Unions

Learn how to indicate all of the possible values that a newly created type can represent.

We'll cover the following

Overview

Algebraic data types (ADTs) in functional programming can be extremely useful. We’ll discuss them in detail later on. However, in TypeScript, we can use discriminating unions (also called tagged unions) to get sum typesA sum type is a data structure used to hold a value that could take on several different, but fixed, types.. These sum types are a category of ADTs, and creating them does require a bit of ceremony, though.

From TypeScript Handbook, we have the following example:

Get hands-on with 1400+ tech skills courses.