Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rule proposal: Prefer array.at(...) over array[...] as T | undefined #8979

Open
6 tasks done
JoshuaKGoldberg opened this issue Apr 23, 2024 · 2 comments
Open
6 tasks done
Labels
accepting prs Go ahead, send a pull request that resolves this issue enhancement: new plugin rule New rule request for eslint-plugin package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@JoshuaKGoldberg
Copy link
Member

JoshuaKGoldberg commented Apr 23, 2024

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:

const nextOperand = chain[index + 1] as ValidOperand | undefined;

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.

@JoshuaKGoldberg JoshuaKGoldberg added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for maintainers to take a look enhancement: new plugin rule New rule request for eslint-plugin labels Apr 23, 2024
@kirkwaiblinger
Copy link
Member

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?

@bradzacher
Copy link
Member

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!

@bradzacher bradzacher added accepting prs Go ahead, send a pull request that resolves this issue and removed triage Waiting for maintainers to take a look labels Apr 24, 2024
kirkwaiblinger added a commit to kirkwaiblinger/typescript-eslint that referenced this issue May 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepting prs Go ahead, send a pull request that resolves this issue enhancement: new plugin rule New rule request for eslint-plugin package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
Development

No branches or pull requests

3 participants