setSizeLengthChecks
Prefer
Set.sizeover spreading into an array and accessing.length.
✅ This rule is included in the ts logical presets.
Spreading a Set into an array just to access .length is wasteful.
Set has a built-in .size property that provides the same information without creating an intermediate array.
This rule reports when:
- A
Set(eithernew Set()directly or aconstvariable initialized with aSet) is spread into an array literal - The array literal immediately accesses
.length
Examples
Section titled “Examples”const count = [...new Set(items)].length;const uniqueItems = new Set([1, 2, 3]);const count = [...uniqueItems].length;const count = [...new Set(items)].length;const count = new Set(items).size;const uniqueItems = new Set([1, 2, 3]);const count = uniqueItems.size;// Spread is fine when the array is actually usedconst uniqueArray = [...new Set(items)];// Not a Set, so spreading to get length is acceptableconst count = [...items].length;Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If you have augmented globals to modify how Array and/or Set and/or ... spreads behave, this rule may not be for you.
Alternately, if you have a large existing codebase where spreading arrays to gain size is the preferred stylistic convention, it may be difficult to migrate to this rule. You might consider using Flint disable comments and/or configuration file disables for those specific situations instead of completely disabling this rule.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- ESLint:
unicorn/prefer-set-size - Oxlint:
unicorn/prefer-set-size
Made with ❤️🔥 in Boston by
Josh Goldberg and contributors.