You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
An overloaded function cannot call identical overloaded function with its parameters #53318
functioninnerGet(): Record<string,string>;functioninnerGet(id: string): string|null;functioninnerGet(ids: string[]): Record<string,string|null>;functioninnerGet(idOrIds?: string|string[]): Record<string,string|null>{}functionouterGet(): Record<string,string>;functionouterGet(id: string): string|null;functionouterGet(ids: string[]): Record<string,string|null>;functionouterGet(idOrIds?: string|string[]): Record<string,string|null>{returninnerGet(idOrIds);// No overload matches this call.// The call would have succeeded against this implementation, but implementation signatures of overloads are not externally visible.}
🙁 Actual behavior
Trying to call a function with the same overloaded parameters will result in a "no overload matches this call" error.
🙂 Expected behavior
There should be no error. It's pretty clear that the types are compatible.
Ok, thanks. I never came across this in 3 years of using TS and I have been using plenty overloads. It's good to know that, better late then never but I wished the docs mentioned it. This is an issue that will not come up immediately, so there may be plenty of code using overloads that will need to be refactored upon realization that there is a major bug that has been marked as wontfix. I only found: "Always prefer parameters with union types instead of overloads when possible" but I don't think it sufficiently warns against it. I would interpret it as "use union types if you don't need to narrow the return type based on the input arg".
And when I try to implement the same with a conditional type in the return, I find myself between the rock and a hard place. I run into a bug I encountered earlier #52051 . As a matter of fact, my workaround to that bug was using overloads :)
Edit: It seems to be possible to override it with casting after all.
Edit2: Hmm, actually I cannot get the above function to work properly when the argument is not supplied. I will investigate it further and update this if anything applies to the current bug.
Edit3: It's possible to use a combo of overload to deal with the missing parameter and conditional types to deal with the union bug.
Activity
MartinJohns commentedon Mar 17, 2023
Duplicate of #39885 / #1805.
mckravchyk commentedon Mar 17, 2023
Ok, thanks. I never came across this in 3 years of using TS and I have been using plenty overloads. It's good to know that, better late then never but I wished the docs mentioned it. This is an issue that will not come up immediately, so there may be plenty of code using overloads that will need to be refactored upon realization that there is a major bug that has been marked as wontfix. I only found: "Always prefer parameters with union types instead of overloads when possible" but I don't think it sufficiently warns against it. I would interpret it as "use union types if you don't need to narrow the return type based on the input arg".
And when I try to implement the same with a conditional type in the return, I find myself between the rock and a hard place. I run into a bug I encountered earlier #52051 . As a matter of fact, my workaround to that bug was using overloads :)
Edit: It seems to be possible to override it with casting after all.
Playground
Edit2: Hmm, actually I cannot get the above function to work properly when the argument is not supplied. I will investigate it further and update this if anything applies to the current bug.
Edit3: It's possible to use a combo of overload to deal with the missing parameter and conditional types to deal with the union bug.
Playground