Last active
May 28, 2021 12:36
-
-
Save rostegg/024f6c643a8581065c18cc6cf0e5aadc to your computer and use it in GitHub Desktop.
Simple example of how to replace the lack of enums in JS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function* generateIds() { | |
| let i = 0; | |
| while (true) { yield i; i++ } | |
| } | |
| const generator = generateIds(); | |
| const getId = () => { return Object.freeze(generator.next().value) } | |
| const typesList = [ | |
| `TEST`, | |
| `GET`, | |
| `BLA` | |
| ]; | |
| const typesObject = typesList.reduce((o, key) => ({ ...o, [key]: getId()}), {}); | |
| const Types = Object.freeze(typesObject); | |
| console.log(Types.GET); | |
| Types.GET = 15; | |
| console.log(Types.GET); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment