Skip to content

arrayDeleteUnnecessaryCounts

Reports using .length or Infinity as the deleteCount or skipCount argument of Array#splice() or Array#toSpliced().

✅ This rule is included in the ts stylistic preset.

When calling Array#splice(start, deleteCount) or Array#toSpliced(start, skipCount), omitting the second argument will delete or skip all elements after the start index. Using .length or Infinity as this argument is redundant and makes the code less clear.

This rule reports when:

  • The array’s .length property is passed as the second argument
  • Infinity or Number.POSITIVE_INFINITY is passed as the second argument
declare const array: number[];
array.splice(1, array.length);
declare const array: number[];
array.splice(0, Infinity);
declare const array: number[];
array.splice(0, Number.POSITIVE_INFINITY);
declare const array: number[];
array.toSpliced(1, array.length);

This rule is not configurable.

If you prefer the explicit style of passing .length or Infinity to make the intent clearer, you can disable this rule. Some teams may find the explicit version more readable, especially for developers less familiar with the splice method’s default behavior.

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