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: typescript tuples should use [] and arrays .at() for accessing members #6936

Closed
6 tasks done
nicolassanmar opened this issue Apr 19, 2023 · 3 comments
Closed
6 tasks done
Labels
enhancement: new plugin rule New rule request for eslint-plugin package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin wontfix This will not be worked on

Comments

@nicolassanmar
Copy link

nicolassanmar commented Apr 19, 2023

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

There should be a rule that allows enforcing arrays to use array.at(0) to access it's members, while allowing tuples to use tuple[0] (bracket notation) to do so.
This is useful since using bracket notation on an array of type T can return undefined if the number is out of range, but the type that typescript returns is only T.
Using .at() correctly informs the developer about this.

This is not true on tuples, where given a tuple of type [X, Y] accessing the parameters with bracket notation should return something of type X or Y.

Currently there is no rule to enforce this, and I could not find a way to implement this as a custom rule either.

Fail Cases

const array: number[] = [1,2]
array[3] // we should not do this since it could not exist on the element

Pass Cases

const tuple: [number,number] = [1,2]
tuple[1] // we know this exists due to the type of the tuple

const array: number[] = [1,2]
array.at(3) // this can return undefined, so the user gets the correct information

Additional Info

No response

@nicolassanmar nicolassanmar added enhancement: new plugin rule New rule request for eslint-plugin package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for maintainers to take a look labels Apr 19, 2023
@nicolassanmar nicolassanmar changed the title Rule proposal: typescript tuples should access with [] and arrays with .at() Rule proposal: typescript tuples should use [] and arrays .at() for accessing members Apr 19, 2023
@bradzacher
Copy link
Member

The difficult thing about this is that .at still only has pretty recent support - less than 2 years in major browsers. Node 14 doesn't support it and it's still LTS.

Additionally there is a TS compiler option which will make array access safe - noUncheckedIndexAccess. In fact it'll be safer than .at because TS can track the result of checking arr[I] and narrow the type - but TS cannot narrow the type of arr.at(i) as it is a function call.

playground example


That being said - we do have #6401 and the associated PR #6411 which are on the track to doing this style of enforcement.

I'm not sure if we would want to take that the whole way to always using .at though. Cc @JoshuaKGoldberg

@JoshuaKGoldberg
Copy link
Member

Yeah I don't think we should implement this in typescript-eslint core. It's a very particular practice that is mirrored by a TypeScript compiler option - and not (yet?) adopted by the community.

We (linters) have been hurt in the past by implementing rules that enforce not-seen-as-best-by-everyone(-yet?) best practices not adopted by the community. People get real mad if they think we're trying to enforce some seemingly arbitrary code style on them... This one certainly couldn't be enabled in a recommended rule set, and even if we provide it as an option it's unclear yet whether many users would want it.

I'd love to see it implemented in a shared ESLint plugin using APIs described in our custom rule docs. Please do! 😄

@nicolassanmar
Copy link
Author

@bradzacher Thanks! I did not know about noUncheckedIndexedAccess and it does solve the issue I have.

@JoshuaKGoldberg I agree with your points. Also, considering that we have the noUncheckedIndexedAccess, this rule will not make sense.

Thank you both!

@bradzacher bradzacher added wontfix This will not be worked on and removed triage Waiting for maintainers to take a look labels Apr 20, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 28, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement: new plugin rule New rule request for eslint-plugin package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

3 participants