Skip to content
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

Value collisions in string enums result in contradictory typings #57065

Closed
LoeschMaximilian opened this issue Jan 15, 2024 · 2 comments
Closed
Labels
Not a Defect This behavior is one of several equally-correct options

Comments

@LoeschMaximilian
Copy link

πŸ”Ž 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

@RyanCavanaugh RyanCavanaugh added the Not a Defect This behavior is one of several equally-correct options label Jan 16, 2024
@RyanCavanaugh
Copy link
Member

This behavior is still correct; the values are considered interchangeable and are effectively aliases for each other.

// Not an error
const c: Messages.HELLO = Messages.BYE;

@typescript-bot
Copy link
Collaborator

This issue has been marked as "Not a Defect" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Jan 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Not a Defect This behavior is one of several equally-correct options
Projects
None yet
Development

No branches or pull requests

3 participants