Skip to content

[no-unnecessary-condition] False positives with arrays and optional chains #1544

Closed
@romeovs

Description

@romeovs

This

{
  "rules": {
    "@typescript-eslint/no-unnecessary-condition": ["error", "ignoreRhs": true | false] 
  }
}
type Item = {
  type : string
}

function fn (arr : Item[]) : string {
  // eslint complains about the optional chain on the next line
  if (arr[0]?.type === "foo") {
    return 'FOO'
  }
  return 'BAR'
}

Expected Result
No lint because the array item might not exist.

Actual Result
Eslint error, even though there should not be one

Versions

package version
@typescript-eslint/eslint-plugin 2.11.0
@typescript-eslint/parser 2.12.0
TypeScript 3.7.3
ESLint 6.7.2
node 12.13.1
npm 6.12.1

Metadata

Metadata

Assignees

No one assigned

    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