Skip to content

Bug: [no-unnecessary-condition] Does not account for optional chaining on potentially-void values #6261

Closed
@cobaltt7

Description

@cobaltt7

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

https://typescript-eslint.io/play/#ts=4.9.4&sourceType=module&code=DYUwLgBAbghgTgSxgI1BAvBARCgxliGAZwiLEQDsBzCAH2gHsEATAbgFgAoL2RFUAPwA6ANoAGALqsgA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge1tiacTJTIAhtEK0yHJgBNK+SpPRRE0aB2iRwYAL4gtQA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eFYDAruuGAL4g9A

Repro Code

let variable = "abc" as string | void;

variable?.[0];

ESLint Config

module.exports = {
  parser: "@typescript-eslint/parser",
  rules: {
    "@typescript-eslint/no-unnecessary-condition": "error"
  },
};

tsconfig

{
  "compilerOptions": {
    "strict": true
  }
}

Expected Result

No problems

Actual Result

The third line reports an error - Unnecessary optional chain on a non-nullish value.

Additional Info

Related to #5766. That PR did not extend to the optional chaining part of this rule.
This is caused by

return (flags & (ts.TypeFlags.Null | ts.TypeFlags.Undefined)) !== 0;
not checking for void, only null and undefined.

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issuebugSomething isn't workinggood first issueGood for newcomerspackage: 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