-
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
Suggestion: Overloading operator &= for boolean type #8424
Comments
See #5407. |
I'm not sure if #5407 is really related to this. This doesn't seem like it's really about providing user-defined 'operator overloading' but more like fixing an undesirable behavior of the run-time that may have to do with some sort of strange coercion that Javascript does. The problem seems 'deeper' than this as in the JS runtime (including in strict mode); var x = true;
var y = x & true; // y gets value 1; However here TypeScript errors:
Perhaps it could also error with |
I am not sure i understand the proposal. Currently both operators generate an error message. so: var x = true;
x = x & true; // Error: The & operator is not allowed for boolean types. Consider using && instead
x &= true; // Error: The & operator is not allowed for boolean types. Consider using && instead |
You're right, I checked in the browser and noticed in worked but assumed based on the report it didn't error in TS, now I realize I misread it and forgot to actually check it.. :) |
I believe the Perhaps the intention was to propose something like I wonder if that was ever proposed to ES? |
The original post is asking that this, which currently errors: var b = true;
b &= true; Be allowed and emitted to this: var b = true;
b = b && true; So that it would be a boolean; however, I referenced #5407 because it states:
I also disagree with the proposal because |
I understand, however this is not always the case in other languages. In C# I can't see a technical reason not having |
This would require type-directed emit. i.e. the operator will be emitted differently based on what the type system things its type is. TypeScript has avoided type-directed emit. this will also break our isolated module scenarios (i.e transpiling a single file in the browser without knowing the context). No type-directed emit has been a long standing axiom and serves to facilitate a fully-erasable type system, that has been one of the TS Design Goals |
It would be great to overload operator &= for booleans to produce boolean value as well. (If it is possible).
Right now in ES5 when you use
you get numeric value 1 instead of boolean true.
I noticed it because it produces compilation errors.
And since it is probably not possible in pure JS to fix that behaviour, it would be nice to compile it into expression which produces boolean as expected. For example:
(The same would apply for operator |= )
The text was updated successfully, but these errors were encountered: