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

fix(eslint-plugin): [unified-signatures] exempt this from optional parameter overload check #11005

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

jedwards1211
Copy link

@jedwards1211 jedwards1211 commented Mar 28, 2025

PR Checklist

Overview

In signaturesDifferByOptionalOrRestParameter, checks the first parameter of both signatures. If one is a this parameter but not the other, then return undefined instead of kind: 'extra-parameter', so that no error is flagged.

@typescript-eslint
Copy link
Contributor

Thanks for the PR, @jedwards1211!

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 Mar 28, 2025

Deploy Preview for typescript-eslint ready!

Name Link
🔨 Latest commit f4689e4
🔍 Latest deploy log https://app.netlify.com/sites/typescript-eslint/deploys/67eb68ed776ec60008737b8b
😎 Deploy Preview https://deploy-preview-11005--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: 98 (🟢 up 4 from production)
Accessibility: 100 (no change from production)
Best Practices: 100 (no change from production)
SEO: 98 (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 Mar 28, 2025

View your CI Pipeline Execution ↗ for commit f4689e4.

Command Status Duration Result
nx run-many --target=build --exclude website --... ✅ Succeeded 4s View ↗
nx run-many --target=clean ✅ Succeeded 10s View ↗

☁️ Nx Cloud last updated this comment at 2025-04-01 04:32:43 UTC

@jedwards1211 jedwards1211 force-pushed the fix-10982 branch 2 times, most recently from ea91923 to 2532ff3 Compare March 28, 2025 17:18
@jedwards1211 jedwards1211 changed the title fix(unified-signatures): exempt this from optional parameter overload check fix(eslint-plugin): exempt this from optional parameter overload check in unified-signatures Mar 28, 2025
Copy link

codecov bot commented Mar 28, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 88.03%. Comparing base (7fad1a5) to head (f4689e4).

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #11005   +/-   ##
=======================================
  Coverage   88.03%   88.03%           
=======================================
  Files         470      470           
  Lines       16801    16804    +3     
  Branches     4745     4747    +2     
=======================================
+ Hits        14790    14793    +3     
  Misses       1664     1664           
  Partials      347      347           
Flag Coverage Δ
unittest 88.03% <100.00%> (+<0.01%) ⬆️

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

Files with missing lines Coverage Δ
...ages/eslint-plugin/src/rules/unified-signatures.ts 87.64% <100.00%> (+0.22%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@jedwards1211 jedwards1211 changed the title fix(eslint-plugin): exempt this from optional parameter overload check in unified-signatures [unified-signatures] fix(eslint-plugin): exempt this from optional parameter overload check in unified-signatures Mar 28, 2025
@jedwards1211 jedwards1211 changed the title [unified-signatures] fix(eslint-plugin): exempt this from optional parameter overload check in unified-signatures fix(eslint-plugin): [unified-signatures] exempt this from optional parameter overload check Mar 28, 2025
Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯 Great first PR, thanks!

@JoshuaKGoldberg JoshuaKGoldberg added the 1 approval >=1 team member has approved this PR; we're now leaving it open for more reviews before we merge label Mar 31, 2025
@@ -310,6 +314,12 @@ export default createRule<Options, MessageIds>({
const shorter = sig1.length < sig2.length ? sig1 : sig2;
const shorterSig = sig1.length < sig2.length ? a : b;

// If one signature has explicit this type and another doesn't, they can't
// be unified.
if (isThisParam(sig1[0]) !== isThisParam(sig2[0])) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question -

Suggested change
if (isThisParam(sig1[0]) !== isThisParam(sig2[0])) {
if (isThisParam(sig1[0]) || isThisParam(sig2[0])) {

I was kind of thinking we would also want to exempt cases where they both include this, since this: Foo | void isn't equivalent to the overload form (playground). Is that not the case?

Copy link
Author

@jedwards1211 jedwards1211 Mar 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't that break cases that should be flagged for merge like this?

function foo(this: SomeClass)
function foo(this: SomeClass, x: number)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm but using different logic, we could also exempt cases where some but not all signatures have this: void without breaking the above example

Copy link
Author

@jedwards1211 jedwards1211 Mar 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kirkwaiblinger btw does returnsVoid (especially calling it with a function argument) do something tricky in your example that I'm not aware of? It seems to me instead of

overloadedThisVoid.call(returnsVoid(() => 'lol'));

you could just do

overloadedThisVoid.call((() => {})())

Or more explicitly

overloadedThisVoid.call(1 as any as void)

Both of which trigger the same TypeScript error.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, yeah, I guess it would come down to special-casing around void, since, in addition to your example, which I agree with, we'd also want to consider:

function foo(this: Foo, x: number)
function foo(this: Bar, x: number)

can still be

function foo(this: Foo | Bar, x: number)

Wonder if special-casing around void is even relevant for non-this parameters too?

function foo(x: void);
function foo(x: number);

Would have to play around to figure that one out.

Copy link
Author

@jedwards1211 jedwards1211 Mar 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It appears that TS picks the this type from the last overload when it comes to .call, lol.

function foo(this: string): void;
function foo(this: number): void;
function foo() {}

foo.call(1)
foo.call('a') // Argument of type 'string' is not assignable to parameter of type 'number'.(2345)

function bar(this: number): void;
function bar(this: string | boolean): void;
function bar() {}

bar.call(1) // Argument of type 'number' is not assignable to parameter of type 'string | boolean'.(2345)
bar.call('a')
bar.call(true)

Copy link
Author

@jedwards1211 jedwards1211 Mar 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah right, the unsoundness of TS function typing rears its head again
Do you know why TS doesn't treat embedded void expressions as an error the way no-confusing-void-expression does? Would be curious to know the explicit rationale from the TS team.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It appears that TS picks the this type from the last overload when it comes to .call, lol.

Huh. 👀 . Well - that the .bind()/.call()/.apply() types are going over my head at this point 😆

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

possibly related - microsoft/TypeScript#14107, microsoft/TypeScript#33815, microsoft/TypeScript#38353... they seem to have more to do with teh parameter types than the this type, but anyway. Let's not stress too much about bind/call/apply type behavior, I suppose. 🤷

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know why TS doesn't treat embedded void expressions as an error the way no-confusing-void-expression does? Would be curious to know the explicit rationale from the TS team.

Not off the top of my head, but I'm sure you'll get (or more likely be pointed to) a thorough answer if you ask in the TS discord.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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: [unified-signatures] thinks overloads with and without this type annotation can be merged
3 participants