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 and stylisticStrict presets.

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
const values: number[]
values
= [1, 2, 3];
const
const result: number[]
result
=
const values: number[]
values
.
Array<number>.slice(start?: number, end?: number): number[]

Returns a copy of a section of an array. For both start and end, a negative index can be used to indicate an offset from the end of the array. For example, -2 refers to the second to last element of the array.

@paramstart The beginning index of the specified portion of the array. If start is undefined, then the slice begins at index 0.

@paramend The end index of the specified portion of the array. This is exclusive of the element at the index 'end'. If end is undefined, then the slice extends to the end of the array.

slice
(1,
const values: number[]
values
.
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 text: "hello"
text
= "hello";
const
const result: string
result
=
const text: "hello"
text
.
String.slice(start?: number, end?: number): string

Returns a section of a string.

@paramstart The index to the beginning of the specified portion of stringObj.

@paramend The index to the end of the specified portion of stringObj. The substring includes the characters up to, but not including, the character indicated by end. If this value is not specified, the substring continues to the end of stringObj.

slice
(1,
const text: "hello"
text
.
String.length: number

Returns the length of a String object.

length
);
const
const items: number[]
items
= [1, 2, 3];
const
const result: number[]
result
=
const items: number[]
items
.
Array<number>.slice(start?: number, end?: number): number[]

Returns a copy of a section of an array. For both start and end, a negative index can be used to indicate an offset from the end of the array. For example, -2 refers to the second to last element of the array.

@paramstart The beginning index of the specified portion of the array. If start is undefined, then the slice begins at index 0.

@paramend The end index of the specified portion of the array. This is exclusive of the element at the index 'end'. If end is undefined, then the slice extends to the end of the array.

slice
(0,
var Infinity: number
Infinity
);
const
const data: number[]
data
= [1, 2, 3];
const
const result: number[]
result
=
const data: number[]
data
.
Array<number>.slice(start?: number, end?: number): number[]

Returns a copy of a section of an array. For both start and end, a negative index can be used to indicate an offset from the end of the array. For example, -2 refers to the second to last element of the array.

@paramstart The beginning index of the specified portion of the array. If start is undefined, then the slice begins at index 0.

@paramend The end index of the specified portion of the array. This is exclusive of the element at the index 'end'. If end is undefined, then the slice extends to the end of the array.

slice
(0,
var Number: NumberConstructor

An object that represents a number of any kind. All JavaScript numbers are 64-bit floating-point numbers.

Number
.
NumberConstructor.POSITIVE_INFINITY: number

A value greater than the largest number that can be represented in JavaScript. JavaScript displays POSITIVE_INFINITY values as infinity.

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