Skip to content

Bug: [no-unnecessary-condition] false positive with non-null-assertions and noUncheckedIndexedAccess #7693

@kiliancs

Description

@kiliancs

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.2.2&fileType=.ts&code=GYVwdgxgLglg9mABAGwIYCMCmyDy6BWm0AzgBQCwAUIomKgLabEBcixUATjGAOYDaAXSoBKRAG8qNYHA6JSyTFEQxEAXkQAGANzLEAHloMmAOgW8oACx0wA1DdETqNRBATtDjNR6Z8YArZLOHESYMABumAByRqR0jACEwgFOAL5UaZRUoJCwCIjBEKER0YyxRqzsXLwOgcFQIBxIcZjJKUA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge1tiacTJTIAhtEK0ipWkOTJE0fJQ5N0UOdA7RIAGnBgAviD1A&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkRsAqkdABbrQDW6AJgJJHd0ADx4BBaNHQkylPJgCu6ADTgakTFNioAbugBSJYgFlY3eRgpg5ilTSgk5yaHkvX0qgL4gPQA&tokens=false

Repro Code

function labelObjects(
  names: string[]
) {
  for (let i = 0; i < names.length; i++) {
    const name = names[i];
    receiveName(name!);
  }
}

function receiveName(name: string) {
  return name;
}

ESLint Config

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

tsconfig

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

Expected Result

No violation of @typescript-eslint/no-unnecessary-type-assertion

Actual Result

Violation of @typescript-eslint/no-unnecessary-type-assertion:
This assertion is unnecessary since it does not change the type of the expression. 6:17 - 6:22

Additional Info

Somewhat related to #6632 and #7610.

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