π Prefer Set methods for Set operations.
πΌπ« This rule is enabled in the β
recommended config. This rule is disabled in the βοΈ unopinionated config.
π§π‘ This rule is automatically fixable by the --fix CLI option and manually fixable by editor suggestions.
Prefer modern Set methods for composing Sets and checking their relationships.
The built-in methods express set operations directly and avoid spelling out temporary arrays and membership-check loops.
// β
const union = new Set([...set, ...otherSet]);
// β
const union = set.union(otherSet);// β
const intersection = [...set].filter(value => otherSet.has(value));
// β
const intersection = set.intersection(otherSet);// β
const difference = [...set].filter(value => !otherSet.has(value));
// β
const difference = set.difference(otherSet);The new Set([...set, ...otherSet]) case is autofixed only when every spread operand is known to be a Set or ReadonlySet, and the operands are safe to reuse in the replacement.
// β
new Set([...iterable, ...otherIterable]);Intersection and difference filter patterns are reported as suggestions instead of autofixes because bare filters produce arrays. Direct new Set([...set].filter(β¦)) wrappers are also suggestions: intersection() can change Set iteration order, and difference() follows the same opt-in model for consistency. For filter patterns, the rule only reports bare filter calls and direct new Set([...set].filter(β¦)) wrappers.
The rule autofixes membership checks and empty intersection or difference checks when both operands are known to be a Set or ReadonlySet:
// β
const subset = [...set].every(value => otherSet.has(value));
const overlaps = [...set].some(value => otherSet.has(value));
const disjoint = set.intersection(otherSet).size === 0;
const contained = set.difference(otherSet).size === 0;
// β
const subset = set.isSubsetOf(otherSet);
const overlaps = !set.isDisjointFrom(otherSet);
const disjoint = set.isDisjointFrom(otherSet);
const contained = set.isSubsetOf(otherSet);Strict inequality (!== 0), reversed comparisons (0 === set.intersection(otherSet).size), and positive size comparisons (set.intersection(otherSet).size > 0 or 0 < set.intersection(otherSet).size) are also supported. Strict inequality and positive size comparisons negate the corresponding predicate.
Negated membership checks are also supported: [...set].every(value => !otherSet.has(value)) becomes set.isDisjointFrom(otherSet), while [...set].some(value => !otherSet.has(value)) becomes !set.isSubsetOf(otherSet).
Callbacks must be synchronous arrow functions with one identifier parameter and a direct otherSet.has(value) or !otherSet.has(value) expression body. Patterns with optional chaining or computed method access are ignored. These autofixes assume ordinary built-in Set behavior.
For both filter and predicate callbacks, evaluating the Set used for membership checks must not have side effects or depend on the callback parameter.