You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
TypeScript recently implemented the optional chaining operator, but we’ve received user feedback that the behavior of optional chaining (?.) with the non-null assertion operator (!) is extremely counter-intuitive.
I believe there's a similar issue with nullish coalescing not following intuitive order of operations; addition, etc, appear to be prioritized over nullish coalescing, meaning you have to add parenthesis most of the time, rather than on occasion.
TypeScript Version: v3.9.2, v4.0.0-dev.20200615
Search Terms: nullish coalescing
Code
letfoo: number|undefined=10console.log((foo??1)+3)// prints 13, as expectedconsole.log(foo??(1+3))// prints 10, as expectedconsole.log(foo??1+3)// prints 10, not 13... Implementation bug?console.log((foo??0)>=5)// prints trueconsole.log(foo??0>=5)// prints 10... I think this is (sorta) a separate issue https://github.com/microsoft/TypeScript/issues/36393foo=undefined// these examples all work as expectedconsole.log((foo??1)+3)// prints 4console.log(foo??(1+3))// prints 4console.log(foo??1+3)// prints 4console.log((foo??0)>=5)// prints falseconsole.log(foo??0>=5)// prints false
Expected behavior:
?? should be processed before most other operators
Actual behavior:
?? happens last, meaning you must (value ?? valueIfUndefined) + ..., but those parenthesis aren't intuitive.
I believe I have found an issue similar to the one mentioned was fixed here: https://devblogs.microsoft.com/typescript/announcing-typescript-3-9/#breaking-changes
I believe there's a similar issue with nullish coalescing not following intuitive order of operations; addition, etc, appear to be prioritized over nullish coalescing, meaning you have to add parenthesis most of the time, rather than on occasion.
TypeScript Version: v3.9.2, v4.0.0-dev.20200615
Search Terms: nullish coalescing
Code
Expected behavior:
?? should be processed before most other operators
Actual behavior:
?? happens last, meaning you must
(value ?? valueIfUndefined) + ...
, but those parenthesis aren't intuitive.Playground Link: Playground Link
Related Issues:
#36031
https://devblogs.microsoft.com/typescript/announcing-typescript-3-9/#breaking-changes
#36393
The text was updated successfully, but these errors were encountered: