question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

?? and ??= behave differently when they shouldn't

See original GitHub issue

TypeScript 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:open
  • Created 3 years ago
  • Reactions:3
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
ddhorstmancommented, Aug 27, 2021

I’ve run into this bug too - is anyone working on a fix? Here’s another example of the problem in action:

type MaybeNumber = number | undefined;
const a: MaybeNumber = undefined;
const b = 5;
let x: MaybeNumber, y: MaybeNumber, z: MaybeNumber;

// Chained nullish coalescing
x = (x ?? a ?? b);
console.log(x + 1); // 6

// Single nullish assignment
y ??= b;
console.log(y + 1); // 6

// Nullish assignment into nullish coalescing
z ??= a ?? b;
console.log(z + 1); // Object is possibly 'undefined'

playground link

As I understand it, x and z should behave identically. Is this difference in behavior somehow intentional?

0reactions
stevenwdvcommented, Aug 25, 2022

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 ||).

let elem!: Element;

let selector1 =
    (elem.hasAttribute('name') && `[name='${elem.getAttribute('name')!}']`)
    ?? elem.tagName;
const s1: string = selector1;  // Correctly reports that false is not assignable to string

let selector2;
selector2 = elem.hasAttribute('name') && `[name='${elem.getAttribute('name')!}']`;
selector2 ??= elem.tagName;
const s2: string = selector2;  // No error as selector2 has been incorrectly narrowed to string, where there should be!!

It seems as though ??= incorrectly works identical to ||=. This is not a regression, it was apparently always there.

Playground link

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found