Skip to main content

prefer-ts-extras-array-find-last-index

Prefer arrayFindLastIndex from ts-extras over array.findLastIndex(...).

arrayFindLastIndex(...) improves predicate inference in typed arrays.

Targeted pattern scopeโ€‹

This rule focuses on direct array.findLastIndex(predicate) calls that can be migrated to arrayFindLastIndex(array, predicate) with deterministic fixes.

  • array.findLastIndex(predicate) call sites that can use arrayFindLastIndex(array, predicate).

Alias indirection, wrapper helpers, and non-canonical call shapes are excluded to keep arrayFindLastIndex(array, predicate) migrations safe.

What this rule reportsโ€‹

This rule reports array.findLastIndex(predicate) call sites when arrayFindLastIndex(array, predicate) is the intended replacement.

  • array.findLastIndex(predicate) call sites that can use arrayFindLastIndex(array, predicate).

Why this rule existsโ€‹

arrayFindLastIndex standardizes reverse index lookup and keeps call signatures aligned with other ts-extras search helpers.

  • Reverse index scans are explicit at the call site.
  • Search code avoids mixed native/helper patterns.
  • Index-based follow-up logic stays uniform across modules.

โŒ Incorrectโ€‹

const index = monitors.findLastIndex((entry) => entry.id === targetId);

โœ… Correctโ€‹

const index = arrayFindLastIndex(monitors, (entry) => entry.id === targetId);

Behavior and migration notesโ€‹

  • Runtime behavior matches native Array.prototype.findLastIndex.
  • Search still proceeds from right to left.
  • If no element matches, the result is -1.

Additional examplesโ€‹

โŒ Incorrect โ€” Additional exampleโ€‹

const index = logs.findLastIndex((entry) => entry.level === "warn");

โœ… Correct โ€” Additional exampleโ€‹

const index = arrayFindLastIndex(logs, (entry) => entry.level === "warn");

โœ… Correct โ€” Repository-wide usageโ€‹

const retryIndex = arrayFindLastIndex(attempts, (attempt) => !attempt.success);

ESLint flat config exampleโ€‹

import typefest from "eslint-plugin-typefest";

export default [
{
plugins: { typefest },
rules: {
"typefest/prefer-ts-extras-array-find-last-index": "error",
},
},
];

When not to use itโ€‹

Disable this rule if your codebase has standardized on native .findLastIndex().

Package documentationโ€‹

ts-extras package documentation:

ts-extras@0.17.x does not currently expose arrayFindLastIndex in its published API, so there is no canonical source/*.ts link for this helper yet.

Reference links:

Rule catalog ID: R005

Further readingโ€‹

Adoption resourcesโ€‹