Enums
Explore TypeScript enums to define human-readable names for numeric or string values. Understand standard enums, string enums, and const enums, and how they help eliminate magic numbers while improving code clarity and performance.
We'll cover the following...
Introduction to enums
Enums are a special type whose concept is similar to other languages, such as C#, C++, or Java, and provides the solution to the problem of special numbers or special strings.
Enums are used to define a human-readable name for a specific number or string.
Consider the following code:
-
We start by using the
enumkeyword to define an enum namedDoorStateon line 2. Thisenumhas two possible values, eitherOpenorClosed. -
We then have a function named
checkDoorStateon lines 8–23 that has a single parameter namedstateof typeDoorState. This means that the ...