-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
[no-unused-vars] Generic type params are covered by varsIgnorePattern rather than argsIgnorePattern #2803
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
Comments
By further testing I found that it seems generic type parameters are covered by |
I want to emulate these tsconfig.json settings:
If you use the above settings there is no way allowed to ignore local vars but you can still ignore both regular parameters and generic type template parameters by prefixing them with underscore. I find no way to do this with how |
TypeScript's unused vars checker simply ignores any name prefixed with an underscore. You can do the same with both Type parameters matching the Could there have been one option per type of thing? Sure. But each option increases configuration complexity and rule complexity. In my experience across many codebases - the most common config is to set all the ignore patterns to the same thing (usually |
@bradzacher From my experience, tsconfig.json:
Example code: export function youCanWorkAroundParamsWithUnderscoreInTsc<_AndTemplateParams>(_unusedParam: string): void {
const _butYouCanNeverWorkAroundLocalsWIthUnderscore = 1;
} Build result
You can see how This is what I want to replicate, I don't want to open a hole for developers to have ignored locals, only ignored parameters becuase that cannot be avoided sometimes. I guess this is why Are you saying you intentionally designed the rule to work differently than |
Apologies, I was misremembering how it works and playground doesn't work well on mobile.
Simply put: because this isn't This rule (and the entirety of ESLint) has existed for much longer than TS even. |
Yes, I agree that the options should work the same way as for the plain javascript rule, otherwise it would be confusing :-) However, types and therefore parameters to generic types is not part of javascript. So treating them as parameters rather than local vars in the extended typescript rule should not affect the underlying javascript rule? |
The base rule's code simply says "if the variable is an argument, use Up until yesterday (literally), the rule was purely an extension rule that built on top of the base rule's logic to teach it about TS. Hence we could not make a distinction between types and variables - because we couldn't access the logic that made that branch. Can we do it now? Yes - as off yesterday it's now technically possible. Should we? I don't think so. |
Running my example code above at astexplorer with Regarding the effort I could do a PR for the change but I do understand about the maintenance cost. I maintain a few open source projects myself and time is always a constraint. Regarding breaking change I understand that too. Perhaps type parameters could be covered by both ignore options to make it more backwards compatbile. This way it would solve our problem (stated below). It would of course break some more unusual cases. It could of course also be put on hold for a major release. The problem we have is that we used the For what it is worth, I still think treating type parameters as parameters is the "correct" way to do it. I know that sounds opinionated but to justify it I will add it is the opinion of |
The work-around we use at the moment is to allow local ignored local vars but make them more explicit than just underscore:
The reasoning is that you can easily name something underscore without thinking about it and then by accident leave it as unused. The downside is that all unused type params also need this explcit annotation but it is managable as we don't have that many of them. This solution is OK in my opinion however I would still consider it a work-around needed because lack of "correct" behavior from the rule :-). |
Repro
Expected Result
I expected the
_A
parameter not being used to be ignored becuase it is prefixed with an underscore and therefore part ofargsIgnorePattern
.Actual Result
error @typescript-eslint/no-unused-vars : '_A' is defined but never used.
Additional Info
Versions
@typescript-eslint/eslint-plugin
4.8.2
@typescript-eslint/parser
4.8.2
TypeScript
4.0.2
ESLint
7.10.0
node
12.18.3
The text was updated successfully, but these errors were encountered: