Skip to content

[no-unnecessary-condition] ignore array index return #1798

Open
@astorije

Description

@astorije

Consider the following snippet:

const foo = ['a', 'b', 'c']; // type: string[]
const bar = foo[5]; // type: string, value: undefined

// Passes the rule
if (foo[5]) {
  console.log(foo[5]);
}

// Fails the rule
if (bar) {
  console.log(bar);
}

Thanks to @Retsam's work on #1534, the first example passes the linter, however as the author mentions in their PR, it doesn't handle less trivial cases, such as storing the value in a variable (which is very common when you want to use the value being tested in the body of the if).

I realize that supporting this (and other advanced) cases might be a lift. In the meantime, or alternatively, any thoughts on an option to entirely ignore arrays? In the current state, I don't believe there is a way to make the second example above pass the linter without disabling the rule or getting rid of the variable.
I believe such option was mentioned in #1544, for reference.

Versions

package version
@typescript-eslint/eslint-plugin 2.24.0
@typescript-eslint/parser 2.24.0
TypeScript 3.8.3
ESLint 6.8.0
node 12.14.0
npm 6.13.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issueenhancementNew feature or requestpackage: 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