The Library Bookmarks · Notes · Snippets

← Back to the front page Filed

Why do enums transpile into weird, large JS?

javascript

Vanilla JS has no concept of enums, just primitives, functions, and objects, so transpilers do their best at turning it into a thing that behaves exactly like one.

enum RealEnum {
  Primary,
  Secondary,
}

transpiles into:

var RealEnum;
(function (RealEnum) {
  RealEnum[RealEnum["Primary"] = 0] = "Primary";
  RealEnum[RealEnum["Secondary] = 1] = "Secondary";
}) (RealEnum || RealEnum = {}));

Big thanks to my team’s tech lead for explaining this.

Why do enums transpile into weird, large JS?

■