Enumify: better enums for JavaScript
In this blog post, I present enumify, a library for implementing enums in JavaScript . The approach it takes is inspired by Java’s enums. Enum patterns The following is a naive enum pattern for JavaScript: const Color = { RED: 0, GREEN: 1, BLUE: 2, } This implementation has several problems: Logging: If you log an enum value such as Color.RED , you don’t see its name. Type safety: Enum values are not unique, they can be mixed up with other values. Membership check: You can’t easily check whether a given value is an element of Color . We can fix problem #1 by using strings instead of numbers as enum values: const Color = { RED: 'RED', GREEN: 'GREEN', BLUE: 'BLUE', } We additionally get type safety if we use symbols as enum values: const Color = { RED: Symbol('RED'), GREEN: Symbol('GREEN'), BLUE: Symbol('BLUE'), } console.log(String(Color.RED...