How to use the spread syntax and new Set() together with TypeScript?

Sometimes, we want to use the spread syntax and new Set() together with TypeScript.

In this article, we’ll look at how to use the spread syntax and new Set() together with TypeScript.

How to use the spread syntax and new Set() together with TypeScript?

To use the spread syntax and new Set() together with TypeScript, we can set the downlevelIteration option to true is tsconfig.json.

For instance, we write

{
  //...
  "compilerOptions": {
    "downlevelIteration": true
  }
  //...
}

to set the downlevelIteration option to true in tsconfig.json.

Then we can use the spread syntax with sets by writing

const uniques = [...new Set([1, 2, 3, 1, 1])];

as we do with JavaScript without the TypeScript compiler throwing errors.

Conclusion

To use the spread syntax and new Set() together with TypeScript, we can set the downlevelIteration option to true is tsconfig.json.