Before You File a Bug Report Please Confirm You Have Done The Following...
Playground Link
https://typescript-eslint.io/play/#ts=6.0.2&fileType=.tsx&code=C4TwDgpgBAYglhANgEwM5QLxQN4CgpQAWSiA9gPwBcUAdgK4C2ARhAE4Dc%2BUA7qaylShNSpRBACGNTgF9cuMcCgAzBClTV4SNJhzTOuJXygAKAMakaqRQGsIIKKSVQA2gHJiiMq4A0UV735kVwBdKHF0c0tgAEocLhUtVGdbEFDyciw6GmQIFRoIZBkgA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge1tiacTJTIAhtEK0yHJgBNK%2BSpPRRE0aB2iRwYAL4gtQA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA&tokens=false
Repro Code
type Fields = {
hello?: number;
world?: boolean;
}
let fields: Fields = {};
for (const key of ['hello', 'world'] as const) {
fields[key] ??= undefined;
}
ESLint Config
module.exports = {
parser: "@typescript-eslint/parser",
rules: {
"@typescript-eslint/no-unnecessary-condition": "error",
},
};
tsconfig
Expected Result
No error. Replacing the ['hello', 'world'] array with either ['hello'] or ['world'] produces no error.
Actual Result
The fields[key] ??= undefined line causes the error:
Unnecessary conditional, left-hand side of ?? operator is always null or undefined.
Additional Info
No response
Before You File a Bug Report Please Confirm You Have Done The Following...
Playground Link
https://typescript-eslint.io/play/#ts=6.0.2&fileType=.tsx&code=C4TwDgpgBAYglhANgEwM5QLxQN4CgpQAWSiA9gPwBcUAdgK4C2ARhAE4Dc%2BUA7qaylShNSpRBACGNTgF9cuMcCgAzBClTV4SNJhzTOuJXygAKAMakaqRQGsIIKKSVQA2gHJiiMq4A0UV735kVwBdKHF0c0tgAEocLhUtVGdbEFDyciw6GmQIFRoIZBkgA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge1tiacTJTIAhtEK0yHJgBNK%2BSpPRRE0aB2iRwYAL4gtQA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA&tokens=false
Repro Code
ESLint Config
tsconfig
{ "compilerOptions": { "strictNullChecks": true } }Expected Result
No error. Replacing the
['hello', 'world']array with either['hello']or['world']produces no error.Actual Result
The
fields[key] ??= undefinedline causes the error:Unnecessary conditional, left-hand side of
??operator is alwaysnullorundefined.Additional Info
No response