# Disable array index (`ts-compat/no-array-index`) 🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). A bracket on an array is ambiguous: the same syntax reads a value, writes one and creates a hole, and it looks exactly like a lookup on a map. This rule bans index access on arrays and tuples. The fixes are: * `a[n]` → `a.at(n)` * `a[n] = v` → `a.splice(n, 1, v)`, with `[0]` appended when the value of the assignment is used * `a[n] += v` → `a.splice(n, 1, a.at(n) + v)`, or `(a.splice(n, 1, a.at(n) + v), a.at(n))` when the value is used * `a[n]++` → `a.splice(n, 1, a.at(n) + 1)`, with `[0]` for the old value and a trailing `+ 1` for the new one The last two evaluate the array and the index twice, so they are fixed only when both are a variable or a literal. A report comes without a fix for `delete a[n]`, for a bracket inside a destructuring target such as `[a[n]] = b`, and for the logical assignment `a[n] ||= v`. Two differences stay: `splice` returns the element it replaced, so `var x = (a[n] = v)` gives `x` the old value, and `a[n] = v` beyond the last index leaves holes where `splice` does not. ## Rule Details Examples of **incorrect** code for this rule: ```ts var a: number[] = [1, 2, 3]; var first = a[0]; a[1] = 2; var last = a[a.length - 1]; var x = (a[0] = 1); a[2] += 1; a[0]++; ``` Examples of **correct** code for this rule: ```ts var a: number[] = [1, 2, 3]; var first = a.at(0); a.splice(1, 1, 2); var last = a.at(-1); a.splice(2, 1, a.at(2) + 1); var map: Record = {}; map["a"] = 1; var text = "ab"; text.charAt(0); ``` ## When Not To Use It * The code needs the fastest access and `at` or `splice` shows up in a hot path. * The array is indexed by a value which is not a number, such as a numeric enum. ## Further Reading * [MDN Array.prototype.at](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at) * [MDN Array.prototype.splice](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice)