Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
3 tasks done
almeidx opened this issue Nov 24, 2020 · 6 comments · Fixed by #2809
Closed
3 tasks done

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

almeidx opened this issue Nov 24, 2020 · 6 comments · Fixed by #2809
Labels
bug Something isn't working package: scope-manager Issues related to @typescript-eslint/scope-manager

Comments

@almeidx
Copy link

almeidx commented Nov 24, 2020

  • 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
@almeidx almeidx added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Nov 24, 2020
@bradzacher
Copy link
Member

bradzacher commented Nov 24, 2020

Your example shows that num is never reassigned.
Which is aligned exactly with the error message you've included.

I think maybe you're misunderstanding the lint rule and how const works?

The rule isn't saying "num must be assigned to a different variable".
The rule is saying "a different value is never assigned to num".

Example:

// VALID - num is never reassigned
const num = 1;
const foo = num;

// VALID - num is reassigned
let num = 1;
num = 2;

// Invalid - num. Is never reassigned
let num = 1;
let foo = num;

@bradzacher bradzacher added awaiting response Issues waiting for a reply from the OP or another party working as intended Issues that are closed as they are working as intended fix: user error issue was fixed by correcting the configuration / correcting the code and removed triage Waiting for team members to take a look awaiting response Issues waiting for a reply from the OP or another party labels Nov 24, 2020
@bradzacher bradzacher removed the fix: user error issue was fixed by correcting the configuration / correcting the code label Nov 24, 2020
@almeidx
Copy link
Author

almeidx commented Nov 24, 2020

@bradzacher The error I mentioned in the Actual Result was the error from the minimal repro code. The error on my use case is 'xp' is never reassigned. Use 'const' instead.

Anyways, I edited the description of the issue to maybe make it more clear

@bradzacher
Copy link
Member

Why are you assigning to xp! instead of just xp?

@almeidx
Copy link
Author

almeidx commented Nov 24, 2020

Why are you assigning to xp! instead of just xp?

xp is type number | undefined because of the optional chain.
If I don't use the non-nullish assertion operator this error appears:

@bradzacher
Copy link
Member

bradzacher commented Nov 24, 2020

okay... so your example again is incorrect.

Your example should actually be

let xp: number | undefined = 1;
xp! += 1;

If you ensure your initial example and error message are together a completely isolated reproduction of your problem, then we can save all this back-and-forth next time. That's the entire point of the template.

@bradzacher bradzacher added bug Something isn't working package: scope-manager Issues related to @typescript-eslint/scope-manager and removed package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin working as intended Issues that are closed as they are working as intended labels Nov 24, 2020
@bradzacher bradzacher reopened this Nov 24, 2020
@almeidx
Copy link
Author

almeidx commented Nov 24, 2020

Alright, sorry for the trouble. 😅

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 25, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working package: scope-manager Issues related to @typescript-eslint/scope-manager
Projects
None yet
2 participants