sindresorhus/eslint-plugin-unicorn

prefer-set-has doesn't autofix types

Closed

#1,148 opened on Mar 24, 2021

 (1 comment) (1 reaction) (0 assignees)JavaScript (468 forks)user submission
enhancementhelp wanted

Repository metrics

Stars
 (5,022 stars)
PR merge metrics
 (Avg merge 1d 16h) (399 merged PRs in 30d)

Description

When "autofixing" an array to a set, the type definition should be fixed too (or the fix should be skipped).

prefer-set-has

const a: Array<'foo' | 'bar'> = ['foo', 'bar']

for (let i = 0; i < 3; i++) {
  if (a.includes(someString)) {
    console.log(123)
  }
}

This is corrected to:

const a: Array<'foo' | 'bar'> = new Set(['foo', 'bar'])

for (let i = 0; i < 3; i++) {
  if (a.has(someString)) {
    console.log(123)
  }
}

Which is confusing and wrong.

Contributor guide