Specifying Types with Literal Types
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 ...
Get Modern Front-End Development for Rails now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.