-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
fix(eslint-plugin): [no-unnecessary-type-parameters] check mapped constraint types if necessary #9740
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
Conversation
…straint types if necessary
Thanks for the PR, @JoshuaKGoldberg! 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 be8cc27. 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 ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #9740 +/- ##
=======================================
Coverage 87.93% 87.93%
=======================================
Files 403 403
Lines 13796 13796
Branches 4022 4023 +1
=======================================
Hits 12131 12131
Misses 1358 1358
Partials 307 307
Flags with carried forward coverage won't be shown. Click here to find out more.
|
cc @danvk - just as a heads up. I invited you as a collaborator to be able to request you for review on these PRs. No worries if you don't have time 🙂 |
@@ -303,7 +303,7 @@ function collectTypeParameterUsageCounts( | |||
if (properties.length === 0) { | |||
// TS treats mapped types like `{[k in "a"]: T}` like `{a: T}`. | |||
// They have properties, so we need to avoid double-counting. | |||
visitType(type.templateType, false); | |||
visitType(type.templateType ?? type.constraintType, false); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From #9530:
The rule previously descended into the typeParameter. It doesn't need to descend into the constraintType because we get that through another code path. It does need to descend into the templateType, though.
I wish I'd included more details about what "another code path" was and what my example was. Since you touched this more recently, do you have an idea? Double-counting would be problematic for this rule since it would lead to false negatives. On the other hand, if we only require one appearance of T
in the return slot, then we can double-count all we want.
I'd also like to double-check whether this comment has any implications for this change:
One wrinkle is that if the mapped type is something like {[k in 'a']: T}, then TypeScript will treat it like {a: T} and we'll process it as an object type. In this case, we need to avoid also processing it as a mapped type, even though isMappedType returns true.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I ... don't 😅. I couldn't figure out how to construct a test case that shows a flaw in this changed logic.
Same with the mapped types like { [k in 'a'] ... }
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have a deep understanding of this, but, approving for the fact that it seems plausible, and the tests pass.
da02f61
into
typescript-eslint:main
PR Checklist
Overview
When a mapped type is implicitly the return type of a function, and its reference to the type parameter is a constraint, we need to check that too. That'll happen in the case of
type.templateType
not being filled out I think.💖