Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
Closed
Description
Before You File a Bug Report Please Confirm You Have Done The Following...
- I have tried restarting my IDE and the issue persists.
- I have updated to the latest version of the packages.
- I havesearched for related issues and found none that matched my issue.
- I haveread the FAQ and my problem is not listed.
Playground Link
Repro Code
constobj:{prop?:[number]}={};functionsetOnce(value:number):[number]{returnobj.prop??=[value];// ! @typescript-eslint/no-unnecessary-condition: "Unnecessary conditional, expected left-hand side of `??` operator to be possibly null or undefined."}
ESLint Config
module.exports={parser:"@typescript-eslint/parser",rules:{"@typescript-eslint/no-unnecessary-condition":"error",},};
tsconfig
{"compilerOptions": {"exactOptionalPropertyTypes":true,"strictNullChecks":true }}
Expected Result
I expected the expressionobj.prop ??= [value]
to not report an error.
Since the property hasn’t been set yet, it is actually undefined at runtime.
Actual Result
The expressionobj.prop ??= [value]
is reported as an error.
typescript-eslint thinksobj.prop
is non-nullish sinceexactOptionalPropertyTypes
is on. We don’t want to turn it off because we still don’t want to allow statements likeobj.prop = undefined;
.