typescript-eslint: Bug: [no-unnecessary-condition] warns about conditional assignment to index signature

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.5&sourceType=module&code=MYewdgzgLgBAZiEAuGAlApqATgEwDzRYCWYA5gDQxgCuAtgEbpYB8MAvDAN4C+A3ALAAoUJFj0AhlhSESpdjADkAa3QBPBQMFCEIANoSsAXRgB+ExwAMvGAHob8RPsnGiEKiFgkiUIuIA2RABe6DiU9NSwIjjeROD+MOIQEESkYLToYJ5ucH7ipKQhCW7UYGCY6EmSqgB02o4GxgDUHACMmkA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge1tiacTJTIAhtEK0yHJgBNK+SpPRRE0aB2iRwYAL4gtQA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eFYDAruuGAL4g9A

Repro Code

const foo: Record<string, number> = {};
const bar: string = 'key';

foo[bar] ??= 0; // foo[bar] is not initialized, but conditional assignment is flagged as unnecessary.
foo[bar] += 1;

ESLint Config

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

tsconfig

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

Expected Result

No warning.

Actual Result

Unnecessary conditional, expected left-hand side of ?? operator to be possibly null or undefined. 4:1 - 4:9

Additional Info

This is basically https://github.com/typescript-eslint/typescript-eslint/issues/6264 which was fixed in https://github.com/typescript-eslint/typescript-eslint/pull/6452; I guess it resurfaced because the rule now checks logical assignments as of https://github.com/typescript-eslint/typescript-eslint/pull/6594.

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 7
  • Comments: 23 (15 by maintainers)

Commits related to this issue

Most upvoted comments

I’ll try to put together a PR today

Copying the answer from @Josh-Cena here for completeness: 😀

That PR changes the quick info of the write type so it excludes undefined (and the programmatic API always excludes it), which makes this PR even more necessary 😃

@Josh-Cena @bradzacher any news? I see #6762 is open now, do you think it needs much more work?

Filed a bug with TS - https://github.com/microsoft/TypeScript/issues/53303 IMO this should be fixed in TS itself cos even TS behaves differently with assignment operators vs their expanded form. But we’ll see what they say.

If not, then we either must implement our own logic to determine if the LHS might be an unchecked index access or not, or we might just disable checks on ??=. First port of call is the types, then we can make a decision about next steps.

No, stuff.hi ??= 2 is equivalent to stuff.hi ?? (stuff.hi = 2). But the point is, if the type of stuff.hi is always number | undefined, then there’s no way that stuff.hi ??= undefined or stuff.hi = undefined shouldn’t be legal.

the write would only accept number

Yeah, you got that right—this is why TS tells our rule that stuff["hi"] ??= 2 is an unnecessary condition, because stuff.hi in a write position can never be undefined! Our rule would need to be fixed to special-case index signatures.