arrayDeleteUnnecessaryCounts
Reports using
.lengthorInfinityas thedeleteCountorskipCountargument ofArray#splice()orArray#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
.lengthproperty is passed as the second argument InfinityorNumber.POSITIVE_INFINITYis passed as the second argument
Examples
Section titled “Examples”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);declare const array: number[];array.splice(1);declare const array: number[];array.splice(1, 2);declare const array: number[];array.splice(1, array.length - 1);declare const other: number[];declare const array: number[];array.splice(1, other.length);Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”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.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”
Made with ❤️🔥 in Boston by
Josh Goldberg and contributors.