-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Added codefix for numeric literals >= 2 ** 53 #33300
Added codefix for numeric literals >= 2 ** 53 #33300
Conversation
`Number.MAX_SAFE_INTEGER` is `2 ** 53 - 1`, so anything greater than that is a 'dangerous' integer to store as a traditional number. This adds a codefix to suggest converting them to a `bigint` literal.
Note that mixing regular numbers and bigints in the same operation will throw runtime errors. I wonder if there’s a way to warn the user not to apply this fix blindly. Also what does this do for scientific notation literals like |
I would rather this thing not warn if I explicitly say I want type literal
One can't always just easily substitute a "large number" for a "bigint". The two types are inherently incompatible. The same goes for |
I wouldn't mind if the fix was for lossy number literals.
However, It makes me a little sad that this will probably remove |
Nothing. It only adds the warning if the number's length is >= 16 characters:
Per the fourslash test cases in ...but actually, on second thought, there are scientific notations with >=16 characters that would still be caught: 1e000000000000000010; Added an explicit check for
That's a good point, but I think I'd rather discuss & tackle scientific numbers separately from this. Parsing them could be tricky. |
The main thing that concerns me with this is that someone will write a large numeric literal, e.g. a big prime number in a hashing algorithm, and get the codefix. You click the lightbulb, it says you should convert this to bigint, so you do, and all is right the world... except it isn’t because it will now throw a TypeError at runtime if you try to use it in a calculation with the “regular” numbers. I guess what I’m getting at here is, how often will it be safe in practice to simply change a large number to bigint without also needing to refactor all the surrounding code as well? |
Well, no, not likely. TypeScript already provides type safety around let regular = 3; // type 'number'
let deluxe = 3n; // type 'bigint'
// Operator '+' cannot be applied to types 'number' and 'bigint'.
regular + deluxe;
From another perspective, it's not generally safe in practice to use these numbers to begin with. Example: #33298. My understanding of the codefixes is that they're of a lesser 'strictness' than the regular compiler itself - that they're useful utilities to make editing the code easier. That's why this is being surfaced as a suggestion + codefix instead of error, to help with that refactoring. |
True, but if at least 50% of the time I’m going to have to refactor the surrounding code because adding a single character introduced 5 new type errors then the automated fix isn’t actually saving me any time. I would be more at ease if this was made to simply be a suggestion (“consider using bigint here”) rather than a full-fledged codefix, though I don’t know if that’s possible? It’s definitely worth calling out that the huge number literal is unsafe, I’m just not really comfortable with the automated fix is all. |
I think I need better reading comprehension. I thought this was still trying to make lossy number literals an error. It's just recommending a code fix that one is free to ignore. I share @fatcerberus ' thoughts on the matter. Sorry for the brain fart =( |
Wait, this still has the same problem I described from last time, which is that the integer is still "safe" if it can be stored in the mantissa, which could make this annoying. |
@DanielRosenwasser I'm a little confused as to the intended tone of these suggestions then (which I think is also what fatcerberus and AnyhowStep are getting at). There are a lot of suggestions already that target seemingly unnecessary fixes - where is the line between adding a 💡 or not? |
We don't have a great guiding philosophy about it right now, but my comment was less about whether or not we should have the info diagnostic, and more about when it's appropriate to issue this error (and I think it's arguable which is why I thought this was better off as a lint rule). There's nothing wrong with the number 1000000000000000000000000000000 because it can be accurately represented. Maybe it's fine for now though since these messages aren't that invasive? |
Number.MAX_SAFE_INTEGER
is2 ** 53 - 1
, so anything greater than that is a 'dangerous' integer to store as a traditional number. This adds a codefix to suggest converting them to abigint
literal.Fixes #29863.