Enums

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 {
    Rectangle,
    Circle,
    Line,
    Freehand   
}

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 ...

Get TypeScript Essentials 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.