Skip to content
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

[no-unused-vars] report when runtime var is only used within a type (type MyType = typeof varName) #3325

Open
3 tasks done
happycollision opened this issue Apr 28, 2021 · 1 comment · May be fixed by #9330
Open
3 tasks done
Labels
accepting prs Go ahead, send a pull request that resolves this issue enhancement New feature or request package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@happycollision
Copy link

  • 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

{
  "rules": {
    "@typescript-eslint/no-unused-vars": "error"
  }
}
const foo = "bar"

export type Baz = typeof foo

Run it locally via https://github.com/happycollision/eslint-no-unused-vars-typescript-repro

Expected Result
An eslint error that foo is unused. (Just like it would if we removed the type def line.)

Actual Result
No eslint error reported.

Additional Info
If you agree with my thoughts below, then this is a bug. If you disagree, then I guess it is a feature request that'd need some options to opt into it.

I am of the opinion that if you create a runtime variable, you ought to be expected to use that variable within your runtime code. Using a runtime variable for purely type definition purposes means that when your code is compiled (depending on your compiler of choice and options), you'll have an extra variable handing around that does nothing. In all likelihood, this is an error and you meant to use the variable but forgot to add it to whatever other part of your code needed it. This is exactly why I realized that this bug exists. 😄

I cannot think of a single case where you'd want such behavior, but I am totally willing to believe they might exist.

Versions

package version
@typescript-eslint/eslint-plugin 4.22.0
@typescript-eslint/parser 4.22.0
TypeScript 4.2.4
ESLint 7.25.0
node v11.12.0
@happycollision happycollision added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for maintainers to take a look labels Apr 28, 2021
@happycollision happycollision changed the title [no-unused-vars] false negative when var is only used within a type (type MyType = typeof varName) [no-unused-vars] false negative when runtime var is only used within a type (type MyType = typeof varName) Apr 30, 2021
@bradzacher bradzacher added bug Something isn't working and removed triage Waiting for maintainers to take a look labels May 15, 2021
@bradzacher
Copy link
Member

I think this should report in the rule, but it should have a different message.
Something like "value variable foo is only used in a type position", so that it's clear WHY it's being reported on.

@JoshuaKGoldberg JoshuaKGoldberg added the accepting prs Go ahead, send a pull request that resolves this issue label Oct 25, 2021
@Josh-Cena Josh-Cena changed the title [no-unused-vars] false negative when runtime var is only used within a type (type MyType = typeof varName) [no-unused-vars] report when runtime var is only used within a type (type MyType = typeof varName) Jun 5, 2024
@Josh-Cena Josh-Cena added enhancement New feature or request and removed bug Something isn't working labels Jun 5, 2024
StyleShit added a commit to StyleShit/typescript-eslint that referenced this issue Jun 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepting prs Go ahead, send a pull request that resolves this issue enhancement New feature or request package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants