-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Autogenerated Enum or with numbers value are not generating a TS2345 as expected (gives valid use when is not) #51987
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
Comments
Duplicate of #26362, which provides plenty of reasoning for this behaviour. Used search terms: enum number in:title |
@MartinJohns I only searched for open issues, as looks like a bug, talks like a bug and moves like a bug, and can be reproduced from TS 3.3.3 onwards therefore it looks as a non-deal with open issue to me. I see that makes sense in that context, Please can you see why it looks a bug to me and my team, and why this inconsistency might cause people to use convoluted and anti-pattern solutions to avoid enums like this video https://www.youtube.com/watch?v=jjMbPt_H3RQ. Why it make sense to have enums working perfectly in generics, and broken in the most basic use case? Why to make compilation faster? is faster better than accurate in a tool that is main propose is to be accurate? 🤔 |
There are no open issues precisely because it’s not a bug. Numeric enums are often used as bitfields, where you need to be able to combine them like The TS type system is not designed to be mathematically sound or “accurate” as you put it. It is designed to, quoth the design goals, “strike a balance between correctness and productivity”. It is very pragmatically designed and there are a lot of these kinds of inconsistencies as a result. |
What @MartinJohns and @fatcerberus said is true, but also, the behavior is changed to what you’re looking for in 5.0 via #51561. If you update the playground version to "Nightly," every call in your example is an error, as you expect. |
Sadly, this is still not an error in the nightly: let x = 5;
console.log(isSwitchOnNum(x)); // no error It would be amazing if it could eventually be made so that |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Bug Report
When a function receives as argument an Enum with values autogenerated or defined numeric values, It fail to check invalid given arguments allowing to compile broken code.
It works as expected when using Enums with string values, or when Forcing to check against a type that has the numbers as piped values. (type ValidEnumVal = 1 | 2 | 3;) UNLESS those values are populated from the enum (type ValidEnumVal = MyEnum.On | MyEnum.Off)
🔎 Search Terms
is:open label:Bug enum
is:open label:Bug enum number
is:issue is:open label:bug enum 2345
🕗 Version & Regression Information
Can be reproduced with 4.9.4
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
with string works fine
Using | in the type also works (I think internally number Enums should be treated like this case)
FAILS if the numbers are coming form a enm
Using | in the type also works (I think internally number Enums should be treated like this case)
🙂 Expected behavior
The text was updated successfully, but these errors were encountered: