-
Notifications
You must be signed in to change notification settings - Fork 12.8k
?? and ??= behave differently when they shouldn't #40359
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
Comments
I think the following lines exhibit the same behavior that is triggering the error above: let a : number[] | undefined = undefined
let b = (a = a ?? []) // b : never[]
a = undefined
let c = (a ??= []) // c : number[]
// The corresponding behavior without nullability
let d : number[]
let e = (d = []) // e : never[] For the example above to run, the corresponding behavior here would be that the inferred type of both |
Sorry for the delay. But I'm a bit confused too... Seems IMO And there are some changes about the /cc: @RyanCavanaugh |
I've run into this bug too - is anyone working on a fix? Here's another example of the problem in action:
As I understand it, |
Might be related to #40494 |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
@Keyacom you need to set your target to ES2021 or later, otherwise it will get downleveled. |
I wonder why the playground's target ES version is set to ES2017 by default, but okay. |
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 ( 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 |
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: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
The text was updated successfully, but these errors were encountered: