Skip to content

Bug: @typescript-eslint/no-unnecessary-condition false positive hiding actual issue #11316

Closed as not planned
@ryan-tote

Description

@ryan-tote

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=MYewdgzgLgBFCm0DyAjAVgLhgJXqATgCYA80%2BAlmAOYA0MYArgLYrz4DaAugHwwC8MAN4BfAFChIsANbwAnvxgBGAAwBuUaITJ07GbM4wA-IYFd1mxFFRpdczgDoADgwgALABQqAlKqA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Y6RAM0WlqYSNkAC1pkA9gEMkyMswDm6KL2jjokADThseIqVlUa9ZIxasm42rCZNEZFMknRCY8UwAmlfJTdLIKtUhtAF8QYKA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3QAacDUhFYAVSLQ%2Bg9ABMAkkVXoAHmoCC0aOhLCwo9FIC%2BIK0A&tokens=false

Repro Code

const testObj: Record<string, number[]> = {}
const key = 10;

testObj[key] ??= [];

// the below code works fine for some reason which is why you also need 
// the rule @typescript-eslint/prefer-nullish-coalescing to be active for this bug to show
// if (!testObj[key]) {
//   testObj[key] = [];
// }

testObj[key].push(10);

ESLint Config

{
  "rules": {
    "@typescript-eslint/prefer-nullish-coalescing": "error",
    "@typescript-eslint/no-unnecessary-condition": "error"
  }
}

tsconfig

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

Expected Result

Shows an error showing that using a number for the key instead of a string is the issue.

Actual Result

Shows the following error

Unnecessary conditional, expected left-hand side of ?? operator to be possibly null or undefined

despite that not actually being the issue

Additional Info

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething 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-pluginworking as intendedIssues that are closed as they are working as intended

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy