-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
fix(eslint-plugin): [no-unnecessary-type-parameters] cannot assume variables are either type or value #10093
fix(eslint-plugin): [no-unnecessary-type-parameters] cannot assume variables are either type or value #10093
Conversation
…riables are either type or value
Thanks for the PR, @Josh-Cena! typescript-eslint is a 100% community driven project, and we are incredibly grateful that you are contributing to that community. The core maintainers work on this in their personal time, so please understand that it may not be possible for them to review your work immediately. Thanks again! 🙏 Please, if you or your company is finding typescript-eslint valuable, help us sustain the project by sponsoring it transparently on https://opencollective.com/typescript-eslint. |
✅ Deploy Preview for typescript-eslint ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
☁️ Nx Cloud ReportCI is running/has finished running commands for commit 1124f6a. As they complete they will appear below. Click to see the status, the terminal output, and the build insights. 📂 See all runs for this CI Pipeline Execution ✅ Successfully ran 2 targetsSent with 💌 from NxCloud. |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #10093 +/- ##
=======================================
Coverage 86.03% 86.03%
=======================================
Files 428 428
Lines 14930 14930
Branches 4329 4329
=======================================
Hits 12845 12845
Misses 1741 1741
Partials 344 344
Flags with carried forward coverage won't be shown. Click here to find out more.
|
I think that the rule should probably just bail out of this usecase altogether, rather than trying to make it work. I think this for two reasons: (1) realistically it's a SUPER rare case that shouldn't happen in any sane code! (2) value vs type usages are not the simplest thing to deal with and I doubt that the rule has the logic to handle the distinction. For example consider the following signatures: declare function foo<T>(T: string, a: T): typeof T;
declare function foo<T>(T: string, a: typeof T): typeof T;
declare function foo<T>(T: string, a: typeof T): T;
declare function foo<T>(T: string, a: T): T; The first signature has just 1 usage of the type parameter The fourth signature is valid and should be valid in the rule as there is a usage of However there is also another complicated case for the rule to handle: declare function foo<T>(T: T, a: string): typeof T;
declare function foo<T>(T: string, a: T): typeof T; Technically the rule shouldn't report on the first signature here because We also can't just add handling or So with all that said - I think that we should just opt out of handling any type parameter that's both a type AND a value. |
By "opt out", do you mean to assume that they are used more than once? Currently the rule's behavior is completely valid—it only collects usage of the variable in type positions. Check out the deploy preview: https://deploy-preview-10093--typescript-eslint.netlify.app/play#ts=5.5.2&fileType=.tsx&code=CYUwxgNghgTiAEAzArgOzAFwJYHtVJxwB4AVAPgAoSAueAZwxi1QHMAaeKWkgSlowCeABxA5E8EgG4AUKEiwEKdNjwFi5KrQZNWHLvEEixEvgeGjxU2eGhwkaTLnyJCpSjXqNm7Tv3PHebhk5W0UHFWdXDQ9tbz1uUytreTslR1UXdXdueM8dFlNDCwlgmwV7ZSc1N008uN8TPyNLGSA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge1tiacTJTIAhtEK0ipWsRFCAtonyJoqDJCXQO0SODABfELqA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA&tokens=false |
Looks like it is explicitly checked for.... typescript-eslint/packages/eslint-plugin/src/rules/no-unnecessary-type-parameters.ts Lines 140 to 148 in 3f02687
I think this seems like it was just a straightforward bug on my part in https://github.com/typescript-eslint/typescript-eslint/pull/9900/files, and therefore it makes sense to me to move forward with this fix as is, granted that the rule already passes the proposed edge cases without modification, as @Josh-Cena showed. |
b3308ba
into
typescript-eslint:main
PR Checklist
Overview
I thought it was something in the scope manager but it turned out to be really simple: in
setItem<T>(T): T
, the variableT
has two declarations, and it's both a type and a value. Therefore we can't assume that the type parameters declared aren't also available as values.