liveBook · Manning

Enum

As explained in Essential TypeScript 5, Third Edition

An enum value is a named value belonging to an enum.

An enum gives related values names so code can refer to them consistently. Define one with `enum`, then access a member as `EnumName.Member`. An enum can also be used as a type. section 9.4

Use named values in a product list

In `src/index.ts`, `enum Product { Hat, Gloves, Umbrella }` defines three members. A tuple such as `[Product.Hat, 100]` pairs a product with its price; a `switch` on the first tuple value selects the matching price calculation. The names make each case clearer than an unexplained number. section 9.4

Numeric members start at 0 by default and increase by one: `Hat`, `Gloves`, and `Umbrella` have values 0, 1, and 2. You can assign a value explicitly—`Gloves = 20` makes the following `Umbrella` value 21—or use an expression. For a numeric enum, `Product[0]` looks up the name `Hat`. section 9.4.1

A string enum assigns a string to every member, as in `enum City { London = "London", Paris = "Paris" }`. Its values are easier to recognize in logs. String and number members can coexist, though that combination is not widely used. section 9.4.2

Watch for the limits of numeric enums:

  • An enum-typed variable can accept a number with no matching member; looking up its name then yields `undefined`. String enums do not have this assignment problem. section 9.4.3
  • `typeof` sees a numeric enum value as a JavaScript `number`, so it cannot distinguish the two in a type guard. section 9.4.3
  • A regular enum produces a JavaScript object. A `const enum` instead substitutes values at compile time, but cannot support a numeric name lookup such as `Product[0]`. section 9.4.3

Enums also fit application commands: a `Commands` enum supplies the choices for an Inquirer.js prompt and the values checked when a user selects Quit or toggles completed to-do items. When only particular values are acceptable, an enum member can also appear within a literal value type. section 2.3.2 section 9.5.2

How Essential TypeScript 5, Third Edition covers it

How deep it goes
Explains declaration, numeric and string values, generated JavaScript, const enums, and value-checking limitations.
Examples
Demonstrates product prices in tuples and command selection in a to-do application using Inquirer.js.
What it assumes
Assumes familiarity with JavaScript numbers and TypeScript types while defining enum syntax before examining its generated JavaScript.
What you can do after
Build it from scratch with named numeric or string members, then use those members in application decisions.
How it connects
Connects enums to tuples, switch statements, JavaScript type guards, and literal value types.

Also explained in

Compare how 23 books explain enum →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Essential TypeScript 5, Third Edition. The section links point to the parts of the book it draws on.