Skip to content

Bug: [@typescript-eslint/prefer-optional-chain] why can't report error for "data && data.code === 0" #10938

Closed as duplicate of#7170
@gaterking

Description

@gaterking

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=GYVwdgxgLglg9mABFApgZygCgCYEMq4BciA3hHNisWCALYBGKATgL6IA%2BiNANtwJSkAUIkQxgiHPlyIAZDMR4CAOnKVEAXk2IADAJLCRiJiiggmSYLm5oUAbgNsU1lEMNGTZpFCYg7DwSxAA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Y6RAM0WloHsalfkwCG8WmQAWo5uii9o-aJHBgAviHVA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA&tokens=false

Repro Code

function test(data: {code: number} | null) {
  if (data && data.code === 0) {
    return false;
  } else {
    return true;
  }
}

ESLint Config

{
  "rules": {
    "@typescript-eslint/prefer-optional-chain": "error"
  }
}

tsconfig

{
  "compilerOptions": {
    // ...
  }
}

Expected Result

show error

Actual Result

why it show ok? I need the rult give some suggrest to optional-chain

Additional Info

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingduplicateThis issue or pull request already existslocked 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-plugintriageWaiting for team members to take a look

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions