Skip to content

[prefer-const] False positives when using non-nullish assertion operator #2804

Closed
@almeidx

Description

@almeidx
  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have read the FAQ and my problem is not listed.

Repro

// ESLint says: 'num' is never reassigned. Use 'const' instead.
let num = data?.a
num! = 1
// same thing happens with +=, -=, etc
{
  "rules": {
    "prefer-const": "error"
  }
}
// your repro code case
let xp = data?.xp;
/* bunch of unrelated code ... */
xp! = 123;

Expected Result

Nothing, since num is being reassigned

Actual Result

ESLint says 'xp' is never reassigned. Use 'const' instead.

Additional Info

Versions

package version
@typescript-eslint/eslint-plugin 4.6.0
@typescript-eslint/parser 4.6.0
TypeScript 4.1.2
ESLint 7.14.0
node 14.14.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingpackage: scope-managerIssues related to @typescript-eslint/scope-manager

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions