Skip to content

Bug: [no-unnecessary-condition] regression with types that include undefined under noUncheckedIndexedAccess #10509

Closed
@controversial

Description

@controversial

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.5.4&fileType=.tsx&code=C4TwDgpgBAysD2AnaBeKBvAUFKEB2wiAlhAM4D8AXFAEoQDGSAJgDymFF4DmANBlADMArnnoAKAJTUAbvCJMoAXwB8AbkyL1mRnnZR4AIwBW1OElQZsuAsTLUsOHPkIgAjPcEjxEjIqU8rRQDNTEwAejCoIVJOLn0wYCJ4PABDABsoegALFM5YwXTSCDSQKDBEeCYhejIoPHgAWhE8BjJSFMQQBp0mIkTkykxDIwA6Z1sKMZs3chHhUUl1R2XHCKgAPXIrFdXI%2BuBoYCzoZFJ4NOkIBVBIKE56NKEmWoAfKLxngU4r7Z2ocMiEEueCgR3gQi4WX0AFs%2BoluKDjvF%2BqkMtlcnh8kRSFAUqDwNAIIgKohBsMphwyLNxm45l5FpggA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge1tiacTJTIAhtEK0yHJgBNK%2BSpPRRE0aB2iRwYAL4gtQA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eFYDArugDTg2RGwAqkWgALdNADW6ACYBJIjPQAPWQEFo0dCTKUO6XgF8QhoA&tokens=false

Repro Code

type Store = {
  entries?: Record<string, { func(): void }>;
};

const obj: Store = {
  entries: {
    entry1: { func() {} },
  },
};

// using optional chaining falsely produces no-unnecessary-condition:
obj.entries?.entry1?.func();
// even though the type of the `entry1` property includes undefined

// but omitting the optional chaining produces a type error:
obj.entries?.entry1.func();
// 18048: 'obj.entries.entry1' is possibly 'undefined'.

ESLint Config

{
  "rules": {
    "@typescript-eslint/no-unnecessary-condition": "error"
  }
}

tsconfig

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

Expected Result

I expected not to see a noUncheckedIndexedError when performing property access on a nested, possibly-undefined value

Actual Result

no-unnecessary-condition raises an error on the optional chain in the last property access, even though using standard (non-optional) property access raises a type error (see playground)

Additional Info

This example worked correctly in 8.18.0 (before #10460)

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