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

Enum allows numeric keys with leading 0s #58435

Closed
laurenz-rausche opened this issue May 4, 2024 · 5 comments
Closed

Enum allows numeric keys with leading 0s #58435

laurenz-rausche opened this issue May 4, 2024 · 5 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@laurenz-rausche
Copy link

laurenz-rausche commented May 4, 2024

πŸ”Ž Search Terms

"enum leading 0", "enum numeric"

πŸ•— 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/?ts=5.5.0-dev.20240504#code/KYOwrgtgBAKsDOAXAouCBvAUFKAiA+gAy5QC8eAJgPYJQDuVATgNZQCG8UAlolPABZUwAGwq5seYmTxRqtEFV4MW7Tjz6CRFKAsS4ANBNyEp5XMuacARmF4ChonYoNHCARhJn6TS1Bt3NR10XAF9MTDgkVEgAOiIIhBQ0GMIEqOSTNKTY9yA

πŸ’» Code

enum TestEnum{
  "_0" = "does work as it should"
  "0" = " does not work as it should not",
  "00" = "works but should not",
  "01" = " works but should not",
}

TestEnum._0
TestEnum.0
TestEnum.00
TestEnum.01

πŸ™ Actual behavior

An enum can be set with a numeric key when a leading 0 is added.

πŸ™‚ Expected behavior

The enum keys "00", "01", ... should not be allowed to be set, as they are unvalid to write as TestEnum.00, TestEnum.01, ...

Additional information about the issue

No response

@xiBread
Copy link

xiBread commented May 4, 2024

Bracket notation is still valid: TestEnum["00"]

@laurenz-rausche
Copy link
Author

Yeah, but in that sense TestEnum["0"] would also be valid.

@fatcerberus
Copy link

It's not about whether or not you can write TestEnum.Foo (e.g. "Foo Bar" = "works" is also legal) but whether the name of a member could potentially clash with a reverse mapping, so keys that overlap with the canonical string representation of a number are intentionally not allowed.

@jcalz
Copy link
Contributor

jcalz commented May 4, 2024

This isn't a bug: all of the following are intentionally allowed:

enum TestEnum {
  "1e2",
  "3.40",
  ".567",
  "88888888888888888"
}

It's for the reason @fatcerberus said; while those can be parsed as numbers, they are not how those numbers would be represented when converted to strings, so they cannot conflict with reverse mappings. If there were no reverse mappings, all numeric keys would be allowed.

@laurenz-rausche
Copy link
Author

Oh okay! Thank you for the information, and sorry for the wrong assumption and the disturbance :D

@DanielRosenwasser DanielRosenwasser added the Working as Intended The behavior described is the intended behavior; this is not a bug label May 5, 2024
@laurenz-rausche laurenz-rausche closed this as not planned Won't fix, can't repro, duplicate, stale May 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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