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
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
- Downgrade packages for false positive Ref: https://github.com/typescript-eslint/typescript-eslint/issues/6632 Ref: https://github.com/typescript-eslint/typescript-eslint/issues/6635 — committed to upleveled/ical-move-events by karlhorky 4 years ago
- Shorten long recurring events to end date (#193 * Shorten long recurring events to end date * Add missing imports, switch to named imports * Update snapshots * Fail on ESLint warnings * I... — committed to upleveled/ical-move-events by karlhorky 4 years ago
- fix(eslint-plugin): [no-unnecessary-condition] ignore optionally accessing index signatures fixes: #6632 Co-authored-by: Joshua Chen <sidachen2003@gmail.com> — committed to arkapratimc/typescript-eslint by arkapratimc 3 years ago
I’ll try to put together a PR today
Copying the answer from @Josh-Cena here for completeness: 😀
@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 ??= 2is equivalent tostuff.hi ?? (stuff.hi = 2). But the point is, if the type ofstuff.hiis alwaysnumber | undefined, then there’s no way thatstuff.hi ??= undefinedorstuff.hi = undefinedshouldn’t be legal.Yeah, you got that right—this is why TS tells our rule that
stuff["hi"] ??= 2is an unnecessary condition, becausestuff.hiin a write position can never beundefined! Our rule would need to be fixed to special-case index signatures.