8000 Bug: [no-unnecessary-type-assertion] problem with asserting querySelector result · Issue #8388 · typescript-eslint/typescript-eslint · GitHub
[go: up one dir, main page]

Skip to content

Bug: [no-unnecessary-type-assertion] problem with asserting querySelector result #83 8000 88

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

Closed
4 tasks done
HolgerJeromin opened this issue Feb 6, 2024 · 1 comment
Closed
4 tasks done
Labels
package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin working as intended Issues that are closed as they are working as intended

Comments

@HolgerJeromin
Copy link

Before You File a Bug Report Please Confirm You Have Done The Following...

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have searched for related issues and found none that matched my issue.
  • I have read the FAQ and my problem is not listed.

Playground Link

https://typescript-eslint.io/play/#ts=5.1.6&fileType=.tsx&code=MYewdgzgLgBAZiEMC8MAmJgFcC2BTMKAOgCMQ0BPIgRyzwCcKBlPAGz2ChHoAoBYAFAwYAcgDEOCmzz5CMAHzoAlgDcRAGkEBKGAEMIMABIAVALIAZAKLtZsAD4wwWVqwDcgpXBg8EILYOFfImgKdiI0JQgAB1ZdChRREXcBQVBIWBJdegSMbFtScipaBmZpTm5%2BIVEJKRsCWEUItU0BLWTPb0z6fyqu4KhQvHDImLiEkSTBVPBoGEyALxzMXHqCyho6RhZ2cvoAHhMLaxl6%2BUrhcUlpWwVlZu12rx4FnuEF-sHh6Nj41AnkoA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUkACAXAngA4oDG0AlobgLQrzkB2uA9AwPbWwMOIkrIBDaPmoFi1AcmSJoucmwbooM6G2iQQ4MAF8Q2oA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA&tokens=false

Repro Code

const foo = document.body.querySelector(
  '#myelement > div',
) as HTMLElement | null;  // This assertion is unnecessary since it does not change the type of the expression.

if (foo)
  foo.style.display = '';

const bar = document.body.querySelector(
  '#myelement > div',
);
if (bar)
  bar.style.display = ''; // failes because Element has no `style`

const baz = document.body.querySelector<HTMLElement>(
  '#myelement > div',
);
if (baz)
  baz.style.display = '';

ESLint Config

module.exports = {
  parser: "@typescript-eslint/parser",
  rules: {
    "@typescript-eslint/no-unnecessary-type-assertion": "error"
  },
};

tsconfig

{
  "compilerOptions": {
    // ...
  }
}

Expected Result

First example should not complain because the assertion changes the type.

Actual Result

There is an error "This assertion is unnecessary since it does not change the type of the expression." at the first example.

Additional Info

No response

@HolgerJeromin HolgerJeromin added bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Feb 6, 2024
@Josh-Cena
Copy link
Member
Josh-Cena commented Feb 6, 2024

Please see #7207, #528, and possibly others. A generic parameter that's only used in the return type can still be inferred, so you should simply use querySelector<YourElement>() instead.

@Josh-Cena Josh-Cena closed this as not planned Won't fix, can't repro, duplicate, stale Feb 6, 2024
@Josh-Cena Josh-Cena added working as intended Issues that are closed as they are working as intended and removed bug Something isn't working triage Waiting for team members to take a look labels Feb 6, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin working as intended Issues that are closed as they are working as intended
Projects
None yet
Development

No branches or pull requests

2 participants
0