π 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 over manually composing Sets with spread arrays and filters.
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. The rule only reports bare filter calls and direct new Set([...set].filter(β¦)) wrappers.