Skip to content

Value collisions in string enums result in contradictory typingsΒ #57065

Closed as not planned
@LoeschMaximilian

Description

@LoeschMaximilian

πŸ”Ž Search Terms

"enum"

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about Enums

⏯ Playground Link

https://www.typescriptlang.org/play?#code/FAUwdgrgtgBAsiAzoghgcyTA3sGeYASAogDIkDyMAvDAEQBGAniLcAL6iSwLLqY74YAIQCaRanSYt2wAMYB7MIgAuMWRJ6oMiAHSiiAbmBA

πŸ’» Code

enum Messages {
    HELLO = "bye"
}
enum Messages {
    BYE = "bye"
}
const c = Messages.BYE;

πŸ™ Actual behavior

declarations

declare enum Messages {
    HELLO = "bye"
}
declare enum Messages {
    BYE = "bye"
}
declare const c = Messages.HELLO;

πŸ™‚ Expected behavior

declarations

declare enum Messages {
    HELLO = "bye"
}
declare enum Messages {
    BYE = "bye"
}
declare const c = Messages.BYE;

Additional information about the issue

I was experimenting with using a global enum to differentiate messages from a message broker like the BroadcastChannel browser API. I was hoping to get compile-errors when a string enum gets expanded by a key with an already present value. It makes sense, that there is no error at compile-time, but the result should at least be consistent between the JavaScript and .d.ts results.

The idea relates to #53013

Metadata

Metadata

Assignees

No one assigned

    Labels

    Not a DefectThis behavior is one of several equally-correct options

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions