← Back to the front page Filed
Why do enums transpile into weird, large JS?
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.

■