Skip to content

Rule proposal: prefer querySelector generic vs type cast #9058

Closed as not planned
@bschlenk

Description

@bschlenk

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement: new plugin ruleNew rule request for eslint-pluginlocked due to agePlease open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing.package: eslint-pluginIssues related to @typescript-eslint/eslint-pluginwontfixThis will not be worked on

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions