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 have searched for related issues and found none that matched my issue.
- I have read the FAQ and my problem is not listed.
Playground Link
Repro Code
const data: Data | null = getData();
// Checking both that `data` is truthy AND that `data.value` is not null (because 0 is valid)
if (data && data.value !== null) {
console.log("Number: " + data.value);
} else {
console.log("Invalid data.");
}
ESLint Config
module.exports = {
parser: "@typescript-eslint/parser",
"rules": {
"@typescript-eslint/prefer-optional-chain": "error"
}
};
tsconfig
No response
Expected Result
I expect in this case the rule would not suggest using optional chaining, because there is no way to check that data
is truthy and that value
is not null in a single condition.
Actual Result
The rule suggests a fix of data?.value !== null
as the condition, but this condition means we are no longer checking if data
is null. Applying the fix changes the logic and causes downstream errors (see playground).
Additional Info
No response