To convert a Set to Array in TypeScript, you can “use the Array.from() function or Spread operator”.
Method 1: Using Array.from()
The Array.from() method creates a new array instance from an iterable object. Since a Set is iterable, it can be directly passed to Array.from() to create an array.
let mainSet = new Set<string>();
mainSet.add("apple");
mainSet.add("banana");
mainSet.add("cherry");
let mainArray = Array.from(mainSet);
console.log(mainArray)
Output
Additionally, if you want to perform some operations on each item of the Set while converting, you can provide a map function to Array.from():
let squaredArray = Array.from(mainSet, value => value * value);
Method 2: Using the spread operator
The spread operator (…) is a concise way to expand elements of iterable objects (like arrays, strings, and sets) into individual elements.
When converting a Set to an array in TypeScript (or JavaScript), you can use the spread operator inside array literals to spread the set into an array.
let mainSet = new Set();
mainSet.add("apple");
mainSet.add("banana");
mainSet.add("cherry");
let mainArray = [...mainSet];
console.log(mainArray)
Output
[ 'apple', 'banana', 'cherry' ]
That’s it!
Related posts

Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.