const enum (completely inlined enums)

Enums are very useful, but some programs don’t actually need the generated code and would benefit from simply inlining all instances of enum members with their numeric equivalents. The new const enum declaration works just like a regular enum for type safety, but erases completely at compile time.

  1. const enum Suit { Clubs, Diamonds, Hearts, Spades }
  2. var d = Suit.Diamonds;

Compiles to exactly:

  1. var d = 1;

TypeScript will also now compute enum values when possible:

  1. enum MyFlags {
  2. None = 0,
  3. Neat = 1,
  4. Cool = 2,
  5. Awesome = 4,
  6. Best = Neat | Cool | Awesome
  7. }
  8. var b = MyFlags.Best; // emits var b = 7;