typescript-eslint: Rule proposal: Prefer array.at(...) over array[...] as T | undefined
Before You File a Proposal Please Confirm You Have Done The Following…
- I have searched for related issues and found none that match my proposal.
- I have searched the current rule list and found no rules that match my proposal.
- I have read the FAQ and my problem is not listed.
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:
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
- basic impl of https://github.com/typescript-eslint/typescript-eslint/issues/8979 — committed to kirkwaiblinger/typescript-eslint by kirkwaiblinger 3 years ago
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.atwas only added inv16.6.0But 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 | undefinedto.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 | undefinedthen 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:
Personally I would raise a motion to close this rule proposal as too niche.
@OlivierZal no because
const [e] = arris unsafe - it does not encode the possibleundefined.I would expect that the eventual implementation should flag the destructuring code and suggest using
.at