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 querySelector generic vs type cast #9058

Closed
6 tasks done
bschlenk opened this issue May 7, 2024 · 2 comments
Closed
6 tasks done

Rule proposal: prefer querySelector generic vs type cast #9058

bschlenk opened this issue May 7, 2024 · 2 comments
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

@bschlenk
Copy link

bschlenk commented May 7, 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

#9007 was closed before I could comment there. I wasn't aware that querySelector took a generic argument. It'd be nice if there was a lint rule that caught

document.querySelector('something') as HTMLElement

and recommended changing it to

document.querySelector<HTMLElement>('something')

And maybe this could be done in a more generic way, possibly catching all functions that accept a generic but are being cast instead?

Fail Cases

document.querySelector('div') as HTMLDivElement

Pass Cases

document.querySelector<HTMLDivElement>('div')

Additional Info

It would help with these duplicates if this was enabled in the recommended ruleset

#8388, #7207, #5808, #5359, #528

@bschlenk bschlenk 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 May 7, 2024
@Josh-Cena
Copy link
Member

Josh-Cena commented Jun 1, 2024

Doesn't no-unnecessary-type-assertion do exactly that? Why do we need a specific rule to report this pattern? (Note that even if we do, it would not be specific to querySelector, but functions with the signature f<T>(...args): T in general.)

I do think we should add this pattern to the docs, and/or improve the error message, but I'm not sure why any new feature is needed.

@Josh-Cena Josh-Cena added awaiting response Issues waiting for a reply from the OP or another party and removed triage Waiting for maintainers to take a look labels Jun 1, 2024
@Josh-Cena
Copy link
Member

Closing because this is exactly what no-unnecessary-type-assertion does. #6951 will improve the error message

@Josh-Cena Josh-Cena closed this as not planned Won't fix, can't repro, duplicate, stale Jun 5, 2024
@Josh-Cena Josh-Cena added wontfix This will not be worked on and removed awaiting response Issues waiting for a reply from the OP or another party labels Jun 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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

2 participants