Skip to content

Bug: [no-unnecessary-condition] doesn't report on unnecessary optional array index access when noUncheckedIndexedAccess is enabled #10960

Closed
@ronami

Description

@ronami

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.8.2&fileType=.tsx&code=CYUwxgNghgTiAEYD2A7AzgF3hkmBc8AgjDFAJ4A8A3vFAPwGYwCWKA5vAL4B8A3AFD8A9EPgAlEAAckMHMHgAjMvAAqZSSADKYFpKxQ02ABYIkCgFbgszQ9LRpmCiMoDkAVxSgAZqxDAX-DiYANoADAC6AHRQAvzMXvAAFEEYYeF00QCU8FT88PAi8ADyesyoUBCIRlCsrBw2KC5YKOC4aLDKJnB52LipEdG8%2Bfn8nILxSSlpGVDZufmFmkZIbhDyCghw0rJ%2Bim5YwEi4jRh0PVMRMwJjQA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge1tiacTJTIAhtEK0yHJgBNK%2BSpPRRE0aB2iRwYAL4gtQA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3QAacDUhFYAVSLQ%2Bg9ABMAkkVXoAHmoCC0aOhLCwo9FIC%2BIK0A&tokens=false

Repro Code

declare const test: Array<{ a?: string }>;

// Reported by TypeScript as the object is possibly 'undefined'
test[0].a;

if (test[0]?.a) {
  // Optional chaining isn't necessary here
  test[0].a;    
}

if (test[0]?.a) {
  // Should be reported but doesn't?
  test[0]?.a;
}

ESLint Config

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

tsconfig

{
  "compilerOptions": {
    "strictNullChecks": true,
    "noUncheckedIndexedAccess": true
  }
}

Expected Result

I expected the rule to report on unnecessary optional array index access when noUncheckedIndexedAccess is enabled.

Actual Result

The rule seems to fails to catch these unnecessary optional array index access cases when noUncheckedIndexedAccess is enabled.

Additional Info

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issuebugSomething isn't workinglocked due to agePlease open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing.package: eslint-pluginIssues related to @typescript-eslint/eslint-plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions