Skip to content

negativeIndexLengthMethods

Prefer negative index over .length - index for at, slice, splice, and similar methods.

✅ This rule is included in the ts stylisticStrict presets.

Array methods like .at(), .slice(), and .splice() accept negative indices to access elements from the end of an array or string. Using array.slice(array.length - 2) is less readable than array.slice(-2).

const
const values: number[]
values
= [1, 2, 3];
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
(
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
- 2);
const
const values: number[]
values
= [1, 2, 3];
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
(
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
- 2,
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
- 1);
const
const values: number[]
values
= [1, 2, 3];
const values: number[]
values
.at(
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
- 1);
Error ts(2550) ― Property 'at' does not exist on type 'number[]'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2022' or later.
const
const values: number[]
values
= [1, 2, 3];
const values: number[]
values
.
Array<number>.splice(start: number, deleteCount?: number): number[] (+1 overload)

Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements.

@paramstart The zero-based location in the array from which to start removing elements.

@paramdeleteCount The number of elements to remove. Omitting this argument will remove all elements from the start paramater location to end of the array. If value of this argument is either a negative number, zero, undefined, or a type that cannot be converted to an integer, the function will evaluate the argument as zero and not remove any elements.

@returnsAn array containing the elements that were deleted.

splice
(
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
- 1, 1);
const
const values: number[]
values
= [1, 2, 3];
const values: number[]
values
.with(
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
- 1, 99);
Error ts(2550) ― Property 'with' does not exist on type 'number[]'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2023' or later.
const
const values: number[]
values
= [1, 2, 3];
var Array: ArrayConstructor
Array
.
ArrayConstructor.prototype: any[]
prototype
.
Array<any>.slice(start?: number, end?: number): any[]

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
.
CallableFunction.call<number[], [number], any[]>(this: (this: number[], args_0: number) => any[], thisArg: number[], args_0: number): any[]

Calls the function with the specified object as the this value and the specified rest arguments as the arguments.

@paramthisArg The object to be used as the this object.

@paramargs Argument values to be passed to the function.

call
(
const values: number[]
values
,
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
- 2);

This rule is not configurable.

If you prefer explicit .length - index calculations for documentation purposes, or if you have a codebase convention that requires them for readability in certain contexts, you may want to disable this rule.

Made with ❤️‍🔥 around the world by the Flint team and contributors.