typescript-eslint: Bug: [unified-signatures] thinks overloads with and without this type annotation can be merged

Before You File a Bug Report Please Confirm You Have Done The Following…

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have searched for related issues and found none that matched my issue.
  • I have read the FAQ and my problem is not listed.

Playground Link

https://typescript-eslint.io/play/#ts=5.7.2&fileType=.tsx&code=KYDwDg9gTgLgBDAnmYcAKUIFsCWBnYAdRxgAsAlYPCAGwDdgo8AeAFQD44BeOAbwCg4cAPTC4AOkn8Avv37CAVAsEK4AVQJQaAQwB2AEzg4sYGsCzBdMbTBwRdcCADM4AbQzZ8wcQHcSFKloGJgBdXgABGhxdAGs4UhgYMDwALlF9YAYaCBQocSwIAC8cGh1xaABzYUsAWjUAZWF9CABjPGFCYAAjYQApbTptepaoHDAYYUonRksW4GEAcWyu7RoAfQB5LoArYBaYdo9cAmE-MkpqekY8aXEVOA3dObgfVABXMAqobQyECDgAHIQX4AJhBABoXqgWno4Hgzi1SH8EKRUF03iVbLo7nAFMJ%2BKBILA4E43k9bPYXv4LkFrmx2AAKACUKXQmGORGpgSuTHpBPA0HgpPJdgcZwCl2CLA4DMEKPwrKOXgAwvY8DAoG99tB%2BCy2Z4CMRztypXzCYKSWT9qKqcbJXSZXKyAr9RzVbp1ZrtVA4AAfOBkjJOaLAfS6xXsrxGiW03kcPhyqDAGBvKAOARCISiCRSISyWRAA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6WJygM0sQBNaySgHMmAQ3yxoKdFETRoAe2iRwYAL4h1QA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA&tokens=false

Repro Code

export type PromiseWithResolvers<T> = {
  // ...
}

/**
 * Userland implementation of [Promise.withResolvers]{@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/withResolvers}.
 * Once we upgrade to Node 22, we can switch to the builtin.
 */
export function withResolvers<T>(): PromiseWithResolvers<T>
export function withResolvers<T>(
  this: PromiseConstructor
): PromiseWithResolvers<T>
export function withResolvers<T>(
  this: PromiseConstructor | undefined
): PromiseWithResolvers<T> {
  return {
    // ...
  }
}

ESLint Config

module.exports = {
  parser: "@typescript-eslint/parser",
  rules: {
    "@typescript-eslint/unified-signatures": "error",
  },
};

tsconfig

{
  "compilerOptions": {
    // ...
  }
}

Expected Result

It’s not possible to merge these overloads, so I expected no error

Actual Result

These overloads can be combined into one signature with an optional parameter. 11:3 - 11:27

Additional Info

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 16 (16 by maintainers)

Most upvoted comments

Wow, okay I’ll work on a PR to close this can of worms. I guess y’all are right to question what’s the point of using this pattern, considering TS doesn’t even give the desired errors in a lot of cases.

I guess there might be an idea of a separate issue to have no-invalid-void-type or some other rule check for overloads with this values that might include void, and request the implementation use this: unknown.

Would preferring unknown over void not be a different rule’s realm though?

Yes… these things are all closely related though so it’s a bit tricky to avoid discussing related things.

Looking again, neither this: void | T nor this: undefined | T are equivalent to the overloaded form. Furthermore, this?: T is a syntax error. Therefore, pedantically, the overloaded form cannot be replaced with any unified signature I can come up with.

(playground)

function overloaded(): void;
function overloaded(this: String): void;
function overloaded() { }

function unionWithUndefined(this: String | undefined): void { }

function unionWithVoid(this: String | void): void { } 

function optionalThis(this?: String) { } // ERROR, comma expected

const o = { overloaded, unionWithUndefined, unionWithVoid }

overloaded();
o.overloaded();
overloaded.bind(returnsVoid(() => 'lol'))(); // ERROR

unionWithUndefined(); // ERROR
o.unionWithUndefined(); // ERROR 
unionWithUndefined.bind(returnsVoid(() => 'lol')); // ERROR

unionWithVoid();
o.unionWithVoid(); // ERROR
unionWithVoid.bind(returnsVoid(() => 'lol'));

function returnsVoid(cb: () => void): void {
  return cb();
}

export { };

Unfortunately, the overloaded form is technically unsafe to implement, since it doesn’t error on o.overloaded(), and therefore, any this value may be provided at runtime. That’s not really this rule’s place to say, but it does sort of force you into writing a safer form.

On balance, I’m now thinking, yes, this is a bug and the this parameter should be exempted from the optional parameter overload check, even though the only cases where this comes up are likely unsafe.

If I understand correctly, undefined is saying… that this must have value undefined, whereas void is saying “don’t use this!” (playground)