Rule proposal: Prefer array.at(...) over array[...] as T | undefined #8979
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
Before You File a Proposal Please Confirm You Have Done The Following...
My proposal is suitable for this project
Description
Array index lookups aren't type-safe without the often-overly-strict
noUncheckedIndexedAccess
: they give backT
, notT | undefined
. A lot of TS code that predatesArray.prototype.at()
had to look like[index] as T | undefined
instead of.at(index)
. Example in our repo:typescript-eslint/packages/eslint-plugin/src/rules/prefer-optional-chain-utils/analyzeChain.ts
Line 97 in eef257b
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
Pass Cases
Additional Info
This is vaguely familiar to the idea of
@typescript-eslint/no-unnecessary-type-assertion
. But it includes a runtime change.The text was updated successfully, but these errors were encountered: