Skip to main content

no-explicit-map-set-loops

💼 This rule is enabled in the ✅ recommended config.

Disallows explicit iteration for maps and sets.

🔧 This rule is automatically fixable by the --fix CLI option.

💭 This rule requires type information.

In this case, "explicit iteration" means using a method like entries or values in a for loop, where omitting the method would result in equivalent code. Forbidding this can make code easier to read.

Also see the no-explicit-array-loops rule.

Rule Details​

In JavaScript/TypeScript, you can iterate over map or set elements implicitly:

for (const [key, value] of myMap) {
}

for (const value of mySet) {
}

Or, you can iterate over map or set elements explicitly:

for (const [key, value] of myMap.entries()) {
}

for (const value of mySet.values()) {
}

Idiomatic TypeScript code iterates implicitly. Explicit iteration is rare because it is needlessly verbose. Thus, it is recommended to forbid this pattern in your codebase to prevent confusion and ensure consistency.

Options​

This rule is not configurable.

Resources​