question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Suggestion: flip the assignment direction of generated enums to save some bits

See original GitHub issue

Search Terms

enum assignment

Suggestion

Flip the assignment order

Use Cases

enums

Examples

enum Action {
  foo
  bar
  baz
  thequickbrownfoxjumpsoverthelazydog
}

currently generates:

"use strict";
var Action;
(function (Action) {
    Action[Action["foo"] = 0] = "foo";
    Action[Action["bar"] = 1] = "bar";
    Action[Action["baz"] = 2] = "baz";
    Action[Action["thequickbrownfoxjumpsoverthelazydog"] = 3] = "thequickbrownfoxjumpsoverthelazydog";
})(Action || (Action = {}));

Suggesting instead:

"use strict";
var Action;
(function (Action) {
    Action[Action[0] = "foo"] = 0;
    Action[Action[1] = "bar"] = 1;
    Action[Action[2] = "baz"] = 2;
    Action[Action[3] = "thequickbrownfoxjumpsoverthelazydog"] = 3;
})(Action || (Action = {}));

Checklist

My suggestion meets these guidelines:

  • This wouldn’t be a breaking change in existing TypeScript/JavaScript code
  • This wouldn’t change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn’t a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript’s Design Goals.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:2
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
dsherretcommented, Nov 10, 2019

@MarkKahn depends on how creative people get 😃

let count = 0;
enum MyEnum {
    A = fn(),
    B = (() => {
        Object.defineProperty(MyEnum, "A", {
            get() {
                count++;
                return count;
            }
        })
        return 5;
    })(),
    C = A,
    D = A
}

Yeah, ok… nobody would do this, but you never know.

1reaction
polkovnikov-phcommented, Nov 9, 2019
let c = 0;
const f = () => c++;
enum A {
    B = f(),
    C = f(),
}

Flipping assignment order leads to incorrect behavior.

Duplicate of #29838 and #19848

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to easily map c++ enums to strings - Stack Overflow
A brute force solution would be a bunch of functions like this, but I feel that's a bit too C-like. enum MyEnum {VAL1,...
Read more >
Handbook - Enums - TypeScript
In this generated code, an enum is compiled into an object that stores both forward ( name -> value ) and reverse (...
Read more >
Enumeration types - C# reference - Microsoft Learn
Learn about C# enumeration types that represent a choice or a combination of choices.
Read more >
C++ Core Guidelines - GitHub Pages
The C++ Core Guidelines are a set of tried-and-true guidelines, rules, and best practices about coding in C++.
Read more >
gcc(1) - Linux manual page - man7.org
If you don't use those parts of the language, you can save some space by using ... the temporary array created for the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found