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
const count: number
count
= [...new
var Set: SetConstructor
new <unknown>(iterable?: Iterable<unknown> | null | undefined) => Set<unknown> (+1 overload)
Set
(
const items: any
items
)].
Array<unknown>.length: number

Gets or sets the length of the array. This is a number one higher than the highest index in the array.

length
;
const
const uniqueItems: Set<number>
uniqueItems
= new
var Set: SetConstructor
new <number>(iterable?: Iterable<number> | null | undefined) => Set<number> (+1 overload)
Set
([1, 2, 3]);
const
const count: number
count
= [...
const uniqueItems: Set<number>
uniqueItems
].
Array<number>.length: number

Gets or sets the length of the array. This is a number one higher than the highest index in the array.

length
;
const
const count: number
count
= [...new
var Set: SetConstructor
new <unknown>(iterable?: Iterable<unknown> | null | undefined) => Set<unknown> (+1 overload)
Set
(
const items: any
items
)].
Array<unknown>.length: number

Gets or sets the length of the array. This is a number one higher than the highest index in the array.

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 ❤️‍🔥 around the world by the Flint team and contributors.