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

Spread arguments length check doesn't work with function overloads #23637

Closed
bowenni opened this issue Apr 24, 2018 · 4 comments
Closed

Spread arguments length check doesn't work with function overloads #23637

bowenni opened this issue Apr 24, 2018 · 4 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@bowenni
Copy link

bowenni commented Apr 24, 2018

TypeScript Version: 2.9.0-dev.20180422

Search Terms: spread, overload

Code

function foo(): void;
function foo(...args: {}[]): void {}

function bar(...args: {}[]) {
  foo(...args);
}

Expected behavior:
No Errors.

Actual behavior:
error TS2556: Expected 0 arguments, but got 1 or more.
If I just swap the order of the declarations of function foo it works.

Playground Link:
https://www.typescriptlang.org/play/#src=function%20foo()%3A%20void%3B%0D%0Afunction%20foo(...args%3A%20%7B%7D%5B%5D)%3A%20void%20%7B%7D%0D%0Afunction%20bar(...args%3A%20%7B%7D%5B%5D)%20%7B%0D%0A%20%20foo(...args)%3B%0D%0A%7D

Related Issues:
#20071 is the fix for excess spread operator check.
#21906 is very close to my issue, but not quite the same.

@mhegazy
Copy link
Contributor

mhegazy commented Apr 24, 2018

The implementation signature is not considered part of the overloads for the method. in other words, foo has one overload that takes no arguments.

if you want to call the function with with variable number of arguments add a new overload:

function foo(): void;
function foo(...args: {}[]);
function foo(...args: {}[]): void {}

@mhegazy mhegazy added the Question An issue which isn't directly actionable in code label Apr 24, 2018
@bowenni
Copy link
Author

bowenni commented Apr 24, 2018

Thanks for the quick reply! Adding a new overload solved the problem for me.

Would you consider The implementation signature is not considered part of the overloads for the method intended or bug? It feels counter intuitive that I have to explicitly declare a new overload even though I have an implementation already.

@AlCalzone
Copy link
Contributor

Having the exposed overloads separate from the implementation allows to expose nice and cleanly typed overloads while having an ugly internal implementation signature that just exists to match all the “official” function signatures.
So definitely a feature IMO

@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@microsoft microsoft locked and limited conversation to collaborators Jul 31, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

4 participants