Skip to content

atAccesses

Prefer using .at() for accessing elements at negative indices.

✅ This rule is included in the ts stylistic and stylisticStrict presets.

Accessing an element at the end of an array or string using array[array.length - 1] is less readable than using the .at() method with a negative index. The .at() method provides a cleaner syntax for accessing elements from the end of a sequence.

const
const last: any
last
=
const array: any
array
[
const array: any
array
.
any
length
- 1];
const
const secondLast: any
secondLast
=
const items: any
items
[
const items: any
items
.
any
length
- 2];
const
const lastChar: any
lastChar
=
const text: any
text
[
const text: any
text
.
any
length
- 1];

This rule is not configurable.

If you need to support environments that do not have the .at() method (pre-ES2022), or if your project has a polyfill that only covers certain cases, you can disable this rule.

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