Book Image

TypeScript Essentials

By : Christopher Nance
Book Image

TypeScript Essentials

By: Christopher Nance

Overview of this book

Table of Contents (15 chapters)


Enums are a useful entity intended for holding a specific value that is referenced using a friendly name to keep code readable. An enum value is nothing more than an integer that is associated with a named constant. Enums are very simple to declare, however, it is what they generate in JavaScript that makes them interesting. First, let's look at an enum declaration and then we will go over the result:

enum ShapeType {

The declaration of an enum type takes the type name for referencing the enum and then the body is just a list of possible values separated by commas. There's nothing particularly special about this from a TypeScript perspective. You can access the enum values like you were accessing a class's static members: ShapeType.Rectangle. As I said earlier though, the real magic behind enums in TypeScript is how they are generated in JavaScript, so let's take a look at the output of our enum:

var ShapeType;
(function (ShapeType...