Skip to content

arraySliceUnnecessaryEnd

Reports unnecessary end argument in .slice() calls when it equals the length or is Infinity.

✅ 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.

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);

This rule is not configurable.

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.

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