Skip to content

Bug: [@typescript-eslint/no-unnecessary-condition] False positive when enable tsconfig noUncheckedIndexedAccess and branded string key #7700

Closed
@yf-yang

Description

@yf-yang

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.0.2&fileType=.tsx&code=C4TwDgpgBAQgTgQwHYBMIoNIRFAvFAZ2DgEskBzKAMigG8oB9AI0VQC4oBXVCAMzPRQAvgG4AUGLLAIcXggDG0ACoQAtmADyTAFYR5wOmKhQA9p2lwA-BwBKek3BQAeeMjSZsAGigBJJEhktXX0APnEhCSkZOUVff0CdPQNaIygyALgOVx4PEHCJeRMkIigEDhV1IKS8OlExQuKDJhqAcgITVWgAa2wW0oJYVncsPPqikvkahAA6MwtLaYBtJgBdBfSZESA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge1tiacTJTIAhtEK0yHJgBNK%2BSpPRgA2uGw5E0aB2iRVEALoBfEIaA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3QAacDUhFYAVSLQ%2Bg9ABMAkkVXoAHmoCC0aOhLCwo9FIC%2BIK0A&tokens=false

Repro Code

type BrandedKey = string & { _brand: undefined };

interface TempObject {
  outer?: Record<BrandedKey, InnerObject>;
}

interface InnerObject {
  inner: BrandedKey;
}

const a: TempObject = {};
const b = 'some key' as BrandedKey;
const c = a.outer?.[b]?.inner;

ESLint Config

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

tsconfig

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

Expected Result

The line

const c = a.outer?.[b]?.inner;

does not have any error

Actual Result

The ?. operator before .inner is reported as an error (false positive)

const c = a.outer?.[b]?.inner;

Additional Info

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issuebugSomething isn't workingpackage: 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