typescript-eslint: Rule proposal: Prefer array.at(...) over array[...] as T | undefined

Before You File a Proposal Please Confirm You Have Done The Following…

My proposal is suitable for this project

  • My proposal specifically checks TypeScript syntax, or it proposes a check that requires type information to be accurate.
  • My proposal is not a “formatting rule”; meaning it does not just enforce how code is formatted (whitespace, brace placement, etc).
  • I believe my proposal would be useful to the broader TypeScript community (meaning it is not a niche proposal).

Description

Array index lookups aren’t type-safe without the often-overly-strict noUncheckedIndexedAccess: they give back T, not T | undefined. A lot of TS code that predates Array.prototype.at() had to look like [index] as T | undefined instead of .at(index). Example in our repo:

https://github.com/typescript-eslint/typescript-eslint/blob/eef257ba5fdd97f56d0d8e42445be6e4c6bb1b60/packages/eslint-plugin/src/rules/prefer-optional-chain-utils/analyzeChain.ts#L97

Now that we have .at(), I think it makes sense to enforce that: if an array index lookup uses a type assertion to add | undefined, it should use .at instead.

Fail Cases

declare const values: string[];
declare const index: number;

values[index] as string | undefined;

Pass Cases

declare const values: string[];
declare const index: number;

values.at(index);

Additional Info

This is vaguely familiar to the idea of @typescript-eslint/no-unnecessary-type-assertion. But it includes a runtime change.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 2
  • Comments: 16 (16 by maintainers)

Commits related to this issue

Most upvoted comments

We didn’t used to use this cos we supported old nodejs versions! Up until our v7 release our min was just v16.0.0 - but .at was only added in v16.6.0 But now that our min is v18 we can safely use this everywhere!

big +1 ❤️

  • Should this also have have any options around prohibiting .at(i)!, or preferring [i] or .at(i) in general? I’m thinking not since .at(i)! is much more useful (e.g. with negative indices), and changing to [i] is a bigger, less safe logic change than [i] as T | undefined to .at(i). And as you noted, noUncheckedIndexAccess, already exists.

  • Due to the runtime changes, let’s be sure to provide a suggested fix, and not an autofix?

eh, yeah, i’m ok with this being closed

If we’re just targetting arr[idx] as T | undefined then I would say that this rule is too niche to be something we should move to implement, personally.

Quickly searching via sourcegraph returns LESS THAN 600 RESULTS.

With that low volume IMO it distinctly fails the final check:

I believe my proposal would be useful to the broader TypeScript community (meaning it is not a niche proposal).

Personally I would raise a motion to close this rule proposal as too niche.

@OlivierZal no because const [e] = arr is unsafe - it does not encode the possible undefined.

I would expect that the eventual implementation should flag the destructuring code and suggest using .at