October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

How to Convert a Set to an Array in TypeScript

Use [...set] to create a new array from a TypeScript Set, or choose Array.from(set) for the same iterable conversion.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use array spread to copy a Set’s values into a new array: const values = [...set];. Array.from(set) is an equally useful alternative. Both preserve the Set’s iteration order and let TypeScript infer the element type.

Convert a Set with array spread

const set = new Set(["red", "green", "blue"]);
const values = [...set];

console.log(values); // ["red", "green", "blue"]

A Set is iterable, so array spread reads its values in iteration order and puts them in a new array. The Set itself remains a Set. TypeScript’s handbook demonstrates this general iterable-to-array pattern as [...xs]: Iterators and Generators.

Use Array.from as an alternative

const values = Array.from(set);

Array.from also creates an array from the Set’s iterable values. Choose whichever form is clearer in context: spread is compact, while Array.from makes the conversion explicit.

What happens to types and values?

TypeScript infers the element type

const numbers = new Set<number>([1, 2]);
const values = [...numbers]; // number[]

An empty Set converts to an empty array with either syntax:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
const empty = new Set<string>();
const values = [...empty]; // []

The array is a shallow copy of the collection. Adding or removing array elements does not add or remove Set entries, but objects in the Set are shared by reference: changing one of those objects is visible through both containers.

Check compiler and runtime support

TypeScript declarations and runtime support are separate concerns. The lib compiler option determines which built-in API declarations TypeScript knows about; the ES2015 library includes declarations for Map and Set, and lib can be configured independently of target. See the TSConfig reference for lib.

Rank #2
TypeScript Programming Language - Software Engineer & Coder T-Shirt
  • TypeScript implements a superset of syntax for strictly typed development, facilitating deep static analysis and enhanced development environment integration. The compiler translates source into standard script formats, ensuring parity across any runtime.
  • TypeScript is ideal for front-end developers, full-stack engineers, and software architects who build large-scale web applications. It serves those looking to improve code excellence, reduce bugs through static checking, and maintain complex projects more.
  • Lightweight, Classic fit, Double-needle sleeve and bottom hem

Declarations do not install APIs in the JavaScript environment. The runtime must support Set, iteration, and Symbol.iterator. TypeScript explains the relationship between target and emitted JavaScript, and how default library declarations vary with the target, in its guide to Type Declarations.

When targeting older JavaScript

The TypeScript handbook notes restrictions on using non-array iterables with for...of when targeting ES5. For older targets, downlevelIteration enables more faithful iterator-protocol handling for constructs such as array spread, but it does not supply a missing runtime Symbol.iterator. The runtime must already provide it or have a suitable shim. See the TypeScript 2.3 release notes.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Leave a Reply

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

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.