prefer-ts-extras-array-at
Prefer arrayAt from ts-extras over array.at(...).
arrayAt(...) preserves stronger element typing for indexed array access.
Targeted pattern scopeβ
This rule focuses on direct array.at(index) calls that can be migrated to arrayAt(array, index) with deterministic fixes.
array.at(index)call sites that can usearrayAt(array, index).
Alias indirection, wrapper helpers, and non-canonical call shapes are excluded to keep arrayAt(array, index) migrations safe.
What this rule reportsβ
This rule reports array.at(index) call sites when arrayAt(array, index) is the intended replacement.
array.at(index)call sites that can usearrayAt(array, index).
Why this rule existsβ
arrayAt keeps indexed access explicit and improves type inference for tuples and readonly arrays.
- Indexing logic is standardized across modules.
- Tuple element access needs fewer local casts.
- Call sites remain explicit about both source array and index.
β Incorrectβ
const firstStatus = statuses.at(0);
β Correctβ
const firstStatus = arrayAt(statuses, 0);
Behavior and migration notesβ
- Runtime semantics align with
Array.prototype.at. - Negative indexes are supported (
-1means last element). - Out-of-range access still returns
undefined.
Additional examplesβ
β Incorrect β Additional exampleβ
const selected = tuple.at(-1); // Weaker tuple index inference
β Correct β Additional exampleβ
const selected = arrayAt(tuple, -1);
β Correct β Repository-wide usageβ
const first = arrayAt(users, 0);
ESLint flat config exampleβ
import typefest from "eslint-plugin-typefest";
export default [
{
plugins: { typefest },
rules: {
"typefest/prefer-ts-extras-array-at": "error",
},
},
];
When not to use itβ
Disable this rule if native .at() usage is required by a local coding standard.
Package documentationβ
ts-extras package documentation:
Source file: source/array-at.ts
/**
Return the item at the given index like `Array#at()`, but with stronger typing for tuples. Supports `-1` on tuples.
This mirrors the runtime behavior of `Array#at()` and returns `undefined` for out-of-bounds indices. For tuples, a negative index of `-1` resolves to the tupleβs last element type. Positive literal indices for tuples resolve to the corresponding element type.
@example
```
import {arrayAt} from 'ts-extras';
const tuple = ['abc', 123, true] as const;
const last = arrayAt(tuple, -1);
//=> true
// ^? true | undefined
const first = arrayAt(tuple, 0);
//=> 'abc'
// ^? 'abc' | undefined
const array = ['a', 'b', 'c'];
const maybeItem = arrayAt(array, -1);
//=> 'c'
// ^? string | undefined
```
@category Improved builtin
*/
Rule catalog ID: R001