# prefer-set-methods πŸ“ Prefer `Set` methods for Set operations. πŸ’ΌπŸš« This rule is enabled in the βœ… `recommended` [config](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config). This rule is _disabled_ in the β˜‘οΈ `unopinionated` [config](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config). πŸ”§πŸ’‘ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix) and manually fixable by [editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions). Prefer modern `Set` methods over manually composing Sets with spread arrays and filters. ## Examples ```js // ❌ const union = new Set([...set, ...otherSet]); // βœ… const union = set.union(otherSet); ``` ```js // ❌ const intersection = [...set].filter(value => otherSet.has(value)); // βœ… const intersection = set.intersection(otherSet); ``` ```js // ❌ 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. ```js // βœ… 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.