-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Closed
Labels
accepting prsGo ahead, send a pull request that resolves this issueGo ahead, send a pull request that resolves this issuebugSomething isn't workingSomething isn't workinglocked due to agePlease open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing.Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing.package: eslint-pluginIssues related to @typescript-eslint/eslint-pluginIssues related to @typescript-eslint/eslint-plugin
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
armano2, guillaumebrunerie, so1ve and kirkwaiblinger
Metadata
Metadata
Assignees
Labels
accepting prsGo ahead, send a pull request that resolves this issueGo ahead, send a pull request that resolves this issuebugSomething isn't workingSomething isn't workinglocked due to agePlease open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing.Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing.package: eslint-pluginIssues related to @typescript-eslint/eslint-pluginIssues related to @typescript-eslint/eslint-plugin