Skip to content

[no-unnecessary-condition] Wrong report "Unnecessary conditional" #2421

Closed
geostyler/geostyler-cli
#28
@juergenzimmermann

Description

@juergenzimmermann
  • [x ] I have tried restarting my IDE and the issue persists.
  • [ x] I have updated to the latest version of the packages.
  • [x ] I have read the FAQ and my problem is not listed.

Repro
The following code wrongly reports Unnecessary conditional, value is always falsy. It's a simplification of several cases where I have if-statements with negations for a function's boolean return value.

const isEven = (val: number) => val % 2 === 0;
if (!isEven(1)) {
    console.log('Odd value');
}

Rule definition in .eslintrc.yml:

rules:
  # ...
  "@typescript-eslint/no-unnecessary-condition": error

tsconfig.json:

{
  "compilerOptions": {
    "target": "ES2019",
    "module": "commonjs",
    "alwaysStrict": true,
    "esModuleInterop": true,
    "experimentalDecorators": true,
    "forceConsistentCasingInFileNames": true,
    "importHelpers": true,
    "inlineSourceMap": true,
    "inlineSources": true,
    "noErrorTruncation": true,
    "noFallthroughCasesInSwitch": true,
    "noImplicitAny": true,
    "noImplicitReturns": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "preserveWatchOutput": true,
    "removeComments": false,
    "skipLibCheck": true,
    "strict": true,
    "strictFunctionTypes": true,
    "strictNullChecks": true,
    "strictPropertyInitialization": true,
    "lib": [
      "ES5",
      "ES2015",
      "ES2016",
      "ES2017",
      "ES2018",
      "ES2019",
      "ES2020",
      "dom"
    ],
    "outDir": "./dist"
  },
  "exclude": ["config", "dist", "test", "node_modules", "scripts", "temp"]
}

Expected Result

No message

Actual Result

Message Unnecessary conditional, value is always falsy.

Versions

package version
@typescript-eslint/eslint-plugin 3.9.2-alpha.10
@typescript-eslint/parser 3.9.2-alpha.10
TypeScript 4.0.2
ESLint 7.7.0
node 14.8.0

Metadata

Metadata

Assignees

Labels

bugSomething isn't workinghas prthere is a PR raised to close thispackage: eslint-pluginIssues related to @typescript-eslint/eslint-plugin

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions