If your class has a literal
member then you can use that
property to discriminate between
union members.
Examples
Exhaustive Checks
Quite commonly you want to make sure that all members of a
union have some code(action)
against them. You can do that by simply adding a fall through and making
sure that the inferred type in that block is compatible with the
never type. For example if you
add the exhaustive check you get a nice error:
Switch
This can also be accomplished with a switch statement:
Exhaustive checks
Another solution is to write a function that only accepts a
never and then throws an
error if its body every executes:
Retrospective Versioning
And after you have a bunch of DTOs you realize that name was a poor
choice. You can add versioning retrospectively by creating a new
union with
literal number (or string if
you want) of DTO. Mark the version 0 as undefined and if you have
strictNullChecks enabled it will just work out: