-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Number literal type allows increment #14745
Comments
(Resubmitting comment as had initially submitted under a different github account name) I happened on precisely this bug today & am pleased to find it's a known problem. @mhegazy I would be willing to help out on this issue if someone with compiler internals knowledge would be kind enough to give me a few pointers on what source files need to be addressed. I have a basic feel for getting around the compiler code having contributed a tiny PR a year or two ago. Thanks for any tips on getting started! |
@indiescripter go for it! Everything you need should be in We also need a new error message like
So go ahead and add that into |
Thanks @DanielRosenwasser for the heads up. I'll give it a go in slow time, in-between other commitments. |
FYI, var s10: 10 | 11 = 10;
s10++; could be construed as probably valid code. We just don't do any similar analysis right now (and made a design choice not to). |
@indiescripter Are you still working on this? Would love to give this a try if otherwise. |
Hi @collin5, thanks for asking. Unfortunately due to some very sad family events a while back I've had to drop all activities like this. Please be my guest and pick it up. It would be a nice & intellectually rewarding task if you want to take it on. Let me know how goes. |
Sorry about that. Will be starting on this soon. Thank you! |
I've created a Pull request to resolve this #28344 |
Some thoughts on this issue... While it doesn't seem unreasonable to disallow increment, decrement, and compound assignment operators on variables of unit types, it also doesn't seem particularly useful. It just isn't meaningful or common to declare mutable variables of unit types (what would be the point?) so we wouldn't be solving a real world problem. The real scenario is the semantics of applying those operators to variables of literal union types, i.e. unions of unit types. I don't think it is feasible to just error here--after all, it is perfectly fine to increment a value of type We have previously decided not to do this because the added complexity (conceptual and in implementation) doesn't justify the marginal gains in type checking. I think this decision still holds, so I'm not sure we really want to do anything here. |
Better suggestion. NO MUTATING operators on ANY literal types. |
TypeScript Version: 2.2.1 / Playground
Code
Expected behavior:
Compiler errors on all three lines
Actual behavior:
Only errors on the last line.
This looks like a clear bug. A bit more subtle case is a union of literals where increment may or may not lead to the correct type. That subtlety should probably be folded into integer types, and ignored for now. Increments and
+=
on literal type should error.The text was updated successfully, but these errors were encountered: