Specifying Types with Literal Types
Learn to specify types with literal types in Typescript in this lesson.
We'll cover the following
Literal types
TypeScript lets you limit a variable that is a string or a number to a set of specific literal values. This literal type is not quite a pure enumeration type, but literal types are pretty similar to enumerations.
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 enum
, but we don’t have anything like that on the client side.
Let’s declare a literal type based on those values. We’ll put it in our venue_reducer
file:
Get hands-on with 1200+ tech skills courses.