sindresorhus/eslint-plugin-unicorn

`no-useless-spread` should report when passing spreading parameters to `new Set`

Chiusa

#2412 aperta il 27 lug 2024

 (7 commenti) (1 reazione) (0 assegnatari)JavaScript (468 fork)user submission
enhancementhelp wanted

Metriche repository

Star
 (5022 stelle)
Metriche merge PR
 (Merge medio 4h 30m) (26 PR mergiate in 30 g)

Descrizione

1️⃣ Explain here what's wrong

function getNames() {
  return ['foo', 'bar', 'foo'];
}

const uniqueNames = [...new Set(...getNames())]; // I mistakenly spreading the `getNames()`
console.log(uniqueNames) // Print [ 'f', 'o' ]. But what I expect is ['foo', 'bar']

no-useless-spread should report error on the code new Set(...getNames()). But actually not.

Because new Set() accept one or zero parameter, we don't need to spread the parameters.

Example

const foo = ['abc'];
const set = new Set(...foo); // BAD!
const set = new Set(foo[0]); // OK

2️⃣ Specify which rule is buggy here and in the title no-useless-spread

3️⃣ More information TypeScript should report error when compiling the code above. Unfortunately, this problem havn't been fixed yet. Set https://github.com/microsoft/TypeScript/issues/59390 and https://github.com/microsoft/TypeScript/issues/48575

Guida contributor