Skip to content

Bug: [no-unnecessary-condition] false positive on indexed access of a generic type #7850

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
mx-bernhard opened this issue Oct 28, 2023 · 1 comment · Fixed by #8235
Closed
4 tasks done
Labels
accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@mx-bernhard
Copy link

mx-bernhard commented Oct 28, 2023

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.2.2&fileType=.ts&code=GYVwdgxgLglg9mABAUwB4EMC2AHANsgHgBUVUpkwATAZ0TgCMArZaAPgAoAoRRdAJwDmALkREANN0QBrZAE8RM2XGCienAJQiAbnBiVEAb0kQE1KInoBGRAF5eggNqKAuogCEdsCFy4A3MdNzegAmW3sBJzlXAH5oxAByYHQfenQIKXj-AF9OEzAzOhEDRGA4OBEzPhgwAQAfLx9ELLDi0vLEBtwm-zQsPGR2ODEEtvj1XyA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge1tiacTJTIAhtEK0yHJgBNK%2BSpPRRE0aB2iRwYAL4gtQA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eFYDArugDTg10NM8AOXapUAYQAW6aAGsylDt160isAKpF2JdABMACgENMh%2BOjxYFYAGaHUOnjShrN2vQBlY0O1dv3lTpAuRNDScnoAkkS66AAeegCC0NDoJFZKvAC%2BIJlAA&tokens=false

Repro Code

function example<T extends object>(
  arg: T,
  key: keyof T  
): void {
  const b1 = arg[key] != null;
  const b2 = arg[key] ?? 'fallback';
}
const o: { foo: string|null } = { foo: null };
example(o, 'foo');

ESLint Config

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

tsconfig

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

Expected Result

No error should be reported since it is possible to supply null values on properties of the incoming object in the example.

Actual Result

An error gets reported saying that null and the value of the type that it is compared with does not have any overlap:

Unnecessary conditional, the types have no overlap. 5:14 - 5:30

Unnecessary conditional, expected left-hand side of `??` operator to be possibly null or undefined. 6:14 - 6:22

Additional Info

No response

@mx-bernhard mx-bernhard 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 Oct 28, 2023
@bradzacher bradzacher added accepting prs Go ahead, send a pull request that resolves this issue and removed triage Waiting for team members to take a look labels Oct 28, 2023
@mx-bernhard
Copy link
Author

I added another case to the issue that is similar, due to specific access of an index access of a generic type variable.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 27, 2024
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 package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
2 participants