Skip to main content

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 use arrayAt(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 use arrayAt(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 (-1 means 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.
*
* @category Improved builtin
*
* @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
* ```;
*/

Rule catalog ID: R001

Further readingโ€‹

Adoption resourcesโ€‹