arrayMutableReverses
Reports
.reverse()calls on arrays that mutate the original array.
✅ This rule is included in the ts stylistic preset.
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 values = [1, 2, 3];const reversed = values.reverse();const items = ["a", "b", "c"];doSomething(items.reverse());const data = [1, 2, 3];const result = [...data].reverse();const values = [1, 2, 3];const reversed = values.toReversed();const items = ["a", "b", "c"];doSomething(items.toReversed());const values = [1, 2, 3];values.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
Made with ❤️🔥 in Boston by
Josh Goldberg and contributors.