?? and ??= behave differently when they shouldn't
See original GitHub issueTypeScript Version: 4.0.2, Nightly (2020-9-2)
Search Terms: Nullish coalescing operator Nullish never Coalescing never Nullish coalescing assignment
Code
According to this post usage of ??
and ??=
should be equivalent in the sample code below:
const values: { a?: string[] } = { };
delete values.a;
(values.a ?? (values.a = [])).push("hello"); // error
values.a = undefined;
(values.a ?? (values.a = [])).push("hello"); // error
delete values.a;
(values.a ??= []).push("hello");
values.a = undefined;
(values.a ??= []).push("hello");
Expected behavior:
Examples with ??
and ??=
should compile and behave similarly.
Actual behavior:
Examples with ??
do not compile (Argument of type ‘string’ is not assignable to parameter of type ‘never’.(2345))
Playground Link: link
Related Issues: Didn’t find anything similar
Issue Analytics
- State:
- Created 3 years ago
- Reactions:3
- Comments:10 (5 by maintainers)
Top Results From Across the Web
Why do we behave differently with different people? Why can't ...
It's very simple. The reason is we have different comfort zone with different people and it is very important to understand. You can...
Read more >Why children behave differently at home | First Five Years
Dr Locke says, firstly, parents shouldn't take it personally. “The five-and-under age group have some difficulty managing their emotions. They' ...
Read more >See differently, think differently, act differently, achieve ...
See differently, think differently, act differently, ... trying something different teaches employees that they shouldn't try new things.
Read more >Complete Guide to Managing Behavior Problems
Learn how to improve the parent-child relationship when it becomes strained with CMI's Parents Guide to Problem Behavior.
Read more >7 Bad Behaviors Parents Should Correct ASAP
These were all different kids of different genders and backgrounds from different families, and in different settings. The only thing they had ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’ve run into this bug too - is anyone working on a fix? Here’s another example of the problem in action:
playground link
As I understand it,
x
andz
should behave identically. Is this difference in behavior somehow intentional?I found a different variant of the bug (posted first as a comment to #40494).
Take the following code snippet, where there is a bug (
??
should have been||
).It seems as though
??=
incorrectly works identical to||=
. This is not a regression, it was apparently always there.Playground link