TS2802

Type 'Set' can only be iterated through when using the '--downlevelIteration' flag or with a '--target' of 'es2015' or higher.

Broken Code ❌

const letters = new Set<string>(['A', 'B', 'C']);
 
for (const letter of letters) {
  console.log(letter);
}
{
  "compilerOptions": {
    // Files
    "rootDir": "src",
    "outDir": "dist",
    // Syntax
    "lib": ["ES2022"],
    "target": "ES5",
    // Modules
    "moduleResolution": "Node16",
    "module": "NodeNext",
    // Type Checking Behaviour
    "strict": true
  }
}

Fixed Code ✔️

In order to resolve the issue, you need to use a higher target environment which supports iterables, such as ES6 for example:

{
  "compilerOptions": {
    // Files
    "rootDir": "src",
    "outDir": "dist",
    // Syntax
    "lib": ["ES2022"],
    "target": "ES6",
    // Modules
    "moduleResolution": "Node16",
    "module": "NodeNext",
    // Type Checking Behaviour
    "strict": true
  }
}

Alternative:

TypeScript will downlevel your code based on your defined "target" to support older JavaScript runtimes that lack built-in iterables. You can enable the downlevelIteration option to ensure compatibility with both legacy and modern platforms. This option generates a helper function that checks if the modern iteration is possible. If it is not supported, the helper function falls back to a legacy iteration, such as index-based iteration.

{
  "compilerOptions": {
    // Files
    "rootDir": "src",
    "outDir": "dist",
    // Syntax
    "lib": ["ES2022"],
    "target": "ES5",
    "downlevelIteration": true,
    // Modules
    "moduleResolution": "Node16",
    "module": "NodeNext",
    // Type Checking Behaviour
    "strict": true
  }
}