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
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)
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-typeor some other rule check for overloads withthisvalues that might includevoid, and request the implementation usethis: unknown.Yes… these things are all closely related though so it’s a bit tricky to avoid discussing related things.
Looking again, neither
this: void | Tnorthis: undefined | Tare equivalent to the overloaded form. Furthermore,this?: Tis a syntax error. Therefore, pedantically, the overloaded form cannot be replaced with any unified signature I can come up with.(playground)
Unfortunately, the overloaded form is technically unsafe to implement, since it doesn’t error on
o.overloaded(), and therefore, anythisvalue 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
thisparameter 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,
undefinedis saying… thatthismust have valueundefined, whereasvoidis saying “don’t use this!” (playground)