Categories
TypeScript Answers

How to convert a set to an array in TypeScript?

Spread the love

Sometimes, we want to convert a set to an array in TypeScript.

In this article, we’ll look at how to convert a set to an array in TypeScript.

How to convert a set to an array in TypeScript?

To convert a set to an array in TypeScript, we can set the downlevelIteration option to true in tsconfig.json‘s compilerOptions.

Then we can use the spread operator to convert sets to arrays.

For instance, we write

{
  "compilerOptions": {
    "downlevelIteration": true
  }
}

to set the downlevelIteration option to true in our project.

Then we can use the spread operator on a set by writing

const arr = [...set];

to convert it to an array.

Conclusion

To convert a set to an array in TypeScript, we can set the downlevelIteration option to true in tsconfig.json‘s compilerOptions.

Then we can use the spread operator to convert sets to arrays.

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 *