Skip to content

setSizeLengthChecks

Prefer Set.size over 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 (either new Set() directly or a const variable initialized with a Set) is spread into an array literal
  • The array literal immediately accesses .length
const count = [...new Set(items)].length;
const uniqueItems = new Set([1, 2, 3]);
const count = [...uniqueItems].length;
const count = [...new Set(items)].length;

This rule is not configurable.

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.

Made with ❤️‍🔥 in Boston by Josh Goldberg and contributors.