Pattern Matching Custom Data Types in TypeScript

Pattern Matching Custom Data Types in TypeScript

The TS compiler constructs a lookup table from our enum keys to their values using a plain JavaScript object. This key, the discriminant, enables TypeScript to distinguish between the object types because its value is a literal value. The implementation looks like this:

It’s worth mentioning that when the type is defined as a union of interfaces, the argument value can be either a POJO or a class instance–both types would satisfy the expected properties.

Source: blog.parametricstudios.com