Categories
TypeScript Answers

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

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *