-
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
Operator '<' cannot be applied to types 'Number' and 'number' #52753
Comments
This is mentioned in the release notes: You can fix this by adding an explicit coercion: |
This code is cursed 🤣
Yeah, it is - but this isn't about comparing a number to a non-number or about comparing a number to a union containing a number and other stuff. It is actually comparing a number to an instance of a Number class. This probably should be supported 🤔 |
Absolutely agreed. I'd definitely smack whoever submitted code like this in a code review.
I guess. We'll see what the team has to say. I'd argue it's a rare corner case and technically it is coercing to |
Yes, it works well, but the implicit conversion seems to be harmless, since it's just a comparison between |
The |
Absolutely I will also not submit such code in teamwork🤣. It's just utility code for leaning purpose, to show how to use prototype, generator function and iterator protocol in JavaScript. On the other hand, is the type |
#2361 is in a tough spot because we'd really like it to happen, but |
@DarrenDanielDay You should definitely coerce Number.prototype[Symbol.iterator] = function* () {
for (let i = 0; i < this; i++) yield i;
}
const v = Number.prototype.valueOf;
Number.prototype.valueOf = function () {
console.log("coerced");
return v.apply(this, arguments);
}
for (const i of 10) console.log(i);
// Logs "coerced" 10 times! This is consistent with how built-in methods normalize input—always coerce at the boundary to minimize number of calls to user code. |
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Bug Report
🔎 Search Terms
Number object
number primitive
binary operator <
ts2365
TypeScript 5.0-dev
🕗 Version & Regression Information
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
In TypeScript 5.0.0-dev.20230210, it reports error
ts2365
, but in TypeScript 4.9.5, it doesn't.🙂 Expected behavior
I'm not sure whether this is a bug of 5.0-dev or a breaking change that which will be introduced in 5.0. If it's a breaking change by design, maybe it should be included in #51362? If not, it should not report any error since it's valid JavaScript code.
Actually, I run into this error when I try to extend
Number.prototype
for utility:The text was updated successfully, but these errors were encountered: