Skip to content

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

JoshuaKGoldberg
Copy link
Member

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.

💖

@typescript-eslint
Copy link
Contributor

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.

Copy link

netlify bot commented Aug 6, 2024

Deploy Preview for typescript-eslint ready!

Name Link
🔨 Latest commit be8cc27
🔍 Latest deploy log https://app.netlify.com/sites/typescript-eslint/deploys/66ba6466853019000833c13c
😎 Deploy Preview https://deploy-preview-9740--typescript-eslint.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 95 (🔴 down 5 from production)
Accessibility: 100 (no change from production)
Best Practices: 92 (no change from production)
SEO: 90 (no change from production)
PWA: 80 (no change from production)
View the detailed breakdown and full score reports

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

nx-cloud bot commented Aug 6, 2024

☁️ Nx Cloud Report

CI 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 targets

Sent with 💌 from NxCloud.

@JoshuaKGoldberg JoshuaKGoldberg requested a review from a team August 6, 2024 19:33
Copy link

codecov bot commented Aug 6, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 87.93%. Comparing base (3d78392) to head (be8cc27).
Report is 29 commits behind head on main.

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           
Flag Coverage Δ
unittest 87.93% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
...plugin/src/rules/no-unnecessary-type-parameters.ts 94.96% <100.00%> (ø)

@JoshuaKGoldberg
Copy link
Member Author

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);
Copy link
Collaborator

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.

Copy link
Member Author

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'] ... }.

Copy link
Member

@kirkwaiblinger kirkwaiblinger left a 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.

@kirkwaiblinger kirkwaiblinger added the 1 approval >=1 team member has approved this PR; we're now leaving it open for more reviews before we merge label Aug 16, 2024
@JoshuaKGoldberg JoshuaKGoldberg merged commit da02f61 into typescript-eslint:main Aug 19, 2024
61 of 62 checks passed
@JoshuaKGoldberg JoshuaKGoldberg deleted the no-unnecessary-type-parameters-check-constraint-types branch August 19, 2024 11:18
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 27, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
1 approval >=1 team member has approved this PR; we're now leaving it open for more reviews before we merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: [no-unnecessary-type-parameters] false positive when parameter is used in inferred mapped return type
3 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy