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

Ambiguously call function with overloads. #55544

Closed
WilcoBakker opened this issue Aug 28, 2023 · 2 comments
Closed

Ambiguously call function with overloads. #55544

WilcoBakker opened this issue Aug 28, 2023 · 2 comments

Comments

@WilcoBakker
Copy link

WilcoBakker commented Aug 28, 2023

🔎 Search Terms

ambiguous, overload, rest, spread, destructing

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about the problem regarding ambiguously calling a function with overloads.

⏯ Playground Link

https://www.typescriptlang.org/play?#code/GYVwdgxgLglg9mABMOcAUBDATgcwIwBciAzlFjGDogD6JggC2ARgKZYA0iAdD9jsUSaoANiwxgA2gF0AlEQBucGABMA3AChQkWAmSo0PLnwGIhcUeOlzEilRq3R4SFOkPGiEkmQpVa9Zmw0piJiYJyGZhaSUpyyiADe6oiIAPQpiAAicCRwDCxQABY+6gC+6prgjrpM2Aa8uCaepOSUQf6sWEGRoeE83ZYxiHGJyRAIpIiewDBYpACCuL1cWCzzDUOIALyIxhrJaZlwq2AA5FCIAO5wWADWSXro07NQCzhLK2v8Mnup6Vmrl2ud2SMGAiDQUAAngAHFhwMFPT5bTbbE79U4yBL3ZIuNCIl6Lbg8D4Er4-EqIFjCYgsLHJHH6fGvd6rUnEb73Mr7P5HYinc5XW73XFuBockpAA

💻 Code

function foo(arg1: string | number, ...args: boolean[]): void;
function foo(...args: boolean[]): void;
function foo(...args: [ string | number | boolean, ...boolean[], ]) {
  // Do something
}

function bar(...args: [ string | number | boolean, ...boolean[], ]) {
  const [ firstArg, ...restArgs ] = args;
  // Doesn't work
  foo(firstArg, ...restArgs);
  // Does work
  if (typeof firstArg === 'boolean') {
    foo(firstArg, ...restArgs);
  } else {
    foo(firstArg, ...restArgs);
  }
  // Doesn't work
  foo(...args);
}

Workbench Repro

🙁 Actual behavior

What happens basically is that TypeScript expects one of the two overload signatures to be used while I guess technically it uses both signatures. It just so happens that it comes down to actually trying to call the function by it's implementation arguments which I am aware are not the signatures of the function itself (which would be the overloaded signatures without body) but I do feel that this is flawed. The reason I feel like this is flawed is because of the example that does work. Which is destructing the args parameter of the bar function to the first argument and the rest arguments that follow, to then check whether the first argument is a boolean or not. If it is a boolean, it calls the function foo the exact same way as it would call the function foo if it weren't a boolean. The last example is to call the function foo with only the arguments as rest parameter which doesn't work either but is actually no different from the first example with destructing.

🙂 Expected behavior

I would expect all three examples to be valid, not because it matches the implementation arguments of the foo function, but because it ambiguously matches two of the overload signatures of the foo function at the same time.

Additional information about the issue

Duplicate of #14107

@jcalz
Copy link
Contributor

jcalz commented Aug 28, 2023

Essentially a duplicate of #14107

@WilcoBakker
Copy link
Author

Thank you @jcalz for referring me to the 'original' issue of this problem. Hopefully this gets resolved one day.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants