Skip to content

[no-unnecessary-condition] Shows optional chaining on index signature type as unnecessary #4954

@SimonSimCity

Description

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

Repro

https://github.com/SimonSimCity/no-unnecessary-condition-bug-4954

{
  "rules": {
    "@typescript-eslint/no-unnecessary-condition": ["error"]
  }
}
interface Property {
  [key: string]: [string] | undefined;
}

// Works correctly
((p: Property) => {
  return p.test?.[0].charAt(2);
})({} as Property);

// Should also not report anything ...
((p: Property | undefined) => {
  return p?.test?.[0].charAt(2);
})(undefined);

My tsconfig:

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es2020",
    "lib": ["es2020"],
    "outDir": "dist",
    "rootDir": ".",
    "sourceMap": true,
    "strict": true,
    "esModuleInterop": true,
    "resolveJsonModule": true
  }
}

Expected Result

Not to report any error.

Actual Result

The rule reports:

Unnecessary optional chain on a non-nullish value. eslint[@typescript-eslint/no-unnecessary-condition](https://typescript-eslint.io/rules/no-unnecessary-condition)

Additional Info

I've started off with older versions of the packages listed below, updated to the mentioned versions, but the problem still persists. Only node I haven't updated, of all packages the mentioned version is the latest as of the time writing.

Versions

package version
@typescript-eslint/eslint-plugin 5.23.0
@typescript-eslint/parser 5.23.0
TypeScript 4.6.4
ESLint 8.15.0
node 14.17.3

Metadata

Metadata

Assignees

No one assigned

    Labels

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