arrayMutableReverses
Reports
.reverse()calls on arrays that mutate the original array.
✅ This rule is included in the tsstylisticandstylisticStrictpresets.
The .reverse() method mutates the original array in place.
Using .toReversed() instead returns a new reversed array without modifying the original, making code more predictable and avoiding unintended side effects.
Examples
Section titled “Examples”const const values: number[]
values = [1, 2, 3];const const reversed: number[]
reversed = const values: number[]
values.Array<number>.reverse(): number[]
Reverses the elements in an array in place.
This method mutates the array and returns a reference to the same array.
reverse();const const items: string[]
items = ["a", "b", "c"];const doSomething: any
doSomething(const items: string[]
items.Array<string>.reverse(): string[]
Reverses the elements in an array in place.
This method mutates the array and returns a reference to the same array.
reverse());const const data: number[]
data = [1, 2, 3];const const result: number[]
result = [...const data: number[]
data].Array<number>.reverse(): number[]
Reverses the elements in an array in place.
This method mutates the array and returns a reference to the same array.
reverse();const const values: number[]
values = [1, 2, 3];const const reversed: number[]
reversed = const values: number[]
values.Array<number>.toReversed(): number[]
Returns a copy of an array with its elements reversed.
toReversed();const const items: string[]
items = ["a", "b", "c"];const doSomething: any
doSomething(const items: string[]
items.Array<string>.toReversed(): string[]
Returns a copy of an array with its elements reversed.
toReversed());const const values: number[]
values = [1, 2, 3];const values: number[]
values.Array<number>.reverse(): number[]
Reverses the elements in an array in place.
This method mutates the array and returns a reference to the same array.
reverse();Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If you intentionally want to mutate the original array in place, or if you’re working in an environment that doesn’t support .toReversed() (ES2023+), you may want to disable this rule.
Note that .reverse() is still allowed when used as a standalone expression statement since the mutation is likely intentional in that case.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- ESLint:
unicorn/no-array-reverse