Skip to content

Bug: [no-unnecessary-condition] Does not account for optional chaining on potentially-void values #6261

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
cobaltt7 opened this issue Dec 21, 2022 · 1 comment · Fixed by #6432
Closed
4 tasks done
Labels
accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working good first issue Good for newcomers package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@cobaltt7
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=4.9.4&sourceType=module&code=DYUwLgBAbghgTgSxgI1BAvBARCgxliGAZwiLEQDsBzCAH2gHsEATAbgFgAoL2RFUAPwA6ANoAGALqsgA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge1tiacTJTIAhtEK0yHJgBNK+SpPRRE0aB2iRwYAL4gtQA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eFYDAruuGAL4g9A

Repro Code

let variable = "abc" as string | void;

variable?.[0];

ESLint Config

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

tsconfig

{
  "compilerOptions": {
    "strict": true
  }
}

Expected Result

No problems

Actual Result

The third line reports an error - Unnecessary optional chain on a non-nullish value.

Additional Info

Related to #5766. That PR did not extend to the optional chaining part of this rule.
This is caused by

return (flags & (ts.TypeFlags.Null | ts.TypeFlags.Undefined)) !== 0;
not checking for void, only null and undefined.

@cobaltt7 cobaltt7 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 Dec 21, 2022
@JoshuaKGoldberg
Copy link
Member

👍 same as #5254 (comment). This is a bug in the rule - though as per #5255 - you really should avoid using | void. See conversation in that issue for why we only reluctantly mark these as accepting PRs 😄

@JoshuaKGoldberg JoshuaKGoldberg added good first issue Good for newcomers accepting prs Go ahead, send a pull request that resolves this issue and removed triage Waiting for team members to take a look labels Dec 21, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 18, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working good first issue Good for newcomers package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
2 participants