June 2021
Intermediate to advanced
398 pages
9h 35m
English
TypeScript lets you limit the values a string or number variable can have to a set of specific literal values. This literal type is not exactly a pure enumeration type, but you can think of it as basically an enumeration type because the behavior is almost identical.
Why would we want to limit the values of a variable? In many cases, we actually have a specific, defined list of values that can be sent to a variable, and we’d like to have TypeScript insist on it. For example, in our concert app, tickets have one of five specific states: unsold, held, purchased, refunded, or invalid. On the Rails side, those values are protected with an ActiveRecord and Postgres enum, but we don’t have anything like that on ...