arraySliceUnnecessaryEnd
Reports unnecessary
endargument in.slice()calls when it equals the length or isInfinity.
✅ This rule is included in the ts stylistic preset.
When calling .slice(start, end) on arrays or strings, omitting the end argument defaults it to the object’s length.
Passing .length or Infinity explicitly as the end argument is unnecessary and reduces readability.
Examples
Section titled “Examples”const values = [1, 2, 3];const result = values.slice(1, values.length);const text = "hello";const result = text.slice(1, text.length);const items = [1, 2, 3];const result = items.slice(0, Infinity);const data = [1, 2, 3];const result = data.slice(0, Number.POSITIVE_INFINITY);const values = [1, 2, 3];const result = values.slice(1);const text = "hello";const result = text.slice(1);const values = [1, 2, 3];const result = values.slice(1, 2);Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If you prefer explicit end arguments for documentation purposes, or if you have a codebase convention that requires them, you may want to disable this rule.
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.