Skip to content

enums showing double the amount of entries; "extra" key value pairs are the flipped versions of the original key-value pairs #51481

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
kptevpro opened this issue Nov 10, 2022 · 5 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@kptevpro
Copy link

Bug Report

πŸ”Ž Search Terms

is:issue enum duplicate key value double extra entries flipped

πŸ•— Version & Regression Information

Found in v~4.7.2

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

enum TransactionTypes {
    A = 1,
    B = 2,
    C = 3,
    D = 4,
    E = 5,
    F = 6,
    G = 7,
    H = 8,
}

console.log('Keys:' + Object.keys(TransactionTypes))
console.log('Values: ' + Object.values(TransactionTypes))

for(let i = 0; i < Object.keys(TransactionTypes).length; i++) {
    console.log(`Pair: ${Object.keys(TransactionTypes)[i]} - ${Object.values(TransactionTypes)[i]}`) 
}

πŸ™ Actual behavior

Enums shows double the amount of entries; shows key-value pairs twice where the second set (extra entries) are the original keys and values 'flipped'.

πŸ™‚ Expected behavior

Show key value pairs once; only one set of the key values declared.

@MartinJohns
Copy link
Contributor

MartinJohns commented Nov 10, 2022

This is working as intended. Check the generated JavaScript. See also #51150.

@fatcerberus
Copy link

For the record the reason for this is so you can evaluate TransactionTypes[x] in the debugger to find out which enum member x (just a number at runtime) refers to.

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Nov 11, 2022
@RyanCavanaugh
Copy link
Member

It would be quite the interesting bug for a 100% accurate reverse map to appear in the output (and go unnoticed for ten years). But no, this is there on purpose.

@RyanCavanaugh RyanCavanaugh closed this as not planned Won't fix, can't repro, duplicate, stale Nov 11, 2022
@aitoraznar
Copy link

Hi, it happened to me when using UPPERCASE keys in the enum.

// Duplicated keys in uppercase && lowercase
enum TransactionTypes {
A = 1,
B = 2,
C = 3,
}

// Correct keys
enum TransactionTypes {
a = 1,
b = 2,
c = 3,
}

@microsoft microsoft locked as resolved and limited conversation to collaborators May 12, 2023
@RyanCavanaugh
Copy link
Member

Please log a new issue with concrete repro steps if you think you've encountered a bug.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

5 participants