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 operator as arguments and optionals + REST in function parameters throws error #11780

Closed
cevek opened this issue Oct 21, 2016 · 7 comments
Labels
Duplicate An existing issue was already created

Comments

@cevek
Copy link

cevek commented Oct 21, 2016

TypeScript Version: 2.0.3

Code

function foo(a?: number, ...args: number[]) {}
foo(...[1,2,3]); // error

var args = [1, 2, 3];
console.log(...args); //error 
@aluanhaddad
Copy link
Contributor

aluanhaddad commented Oct 22, 2016

Your program has an error, it is calling foo with arguments that literally do not match the signature. The same is true about the call to console.log. An optional argument does not merge with a rest argument of a compatible element type because it is a syntactic feature and the parameters are passed by position. How can we distinguish the arguments and refer to them in the body of foo without this behavior?

The correct way to call foo by spreading ...args into the second parameter is

foo(undefined, ...args);
console.log(undefined, ...args);

@cevek
Copy link
Author

cevek commented Oct 22, 2016

Yeah, I understand that don't match the signature, but ts checker may merge arguments if you use spreading

function x(...args: number[]);
function x(a: number, ...args: number[]) {}
x(...[1, 2, 3]); //ok
//this is not correct
console.log(undefined, ...args);

// correct code
console.log(args[0], ...args.slice(1));

@Buslowicz
Copy link

I've got a bit similar issue, basically, spread arguments do not translate to multiple arguments.

function test(reg: RegExp, str: string) {
  console.log("abc".replace(reg, str));
}
test(...[ /b/, "d" ]); // TS2346:Supplied parameters do not match any signature of call target.
function test(...args);
function test(reg: RegExp, str: string) {
  console.log("abc".replace(reg, str));
}
test(...[ /b/, "d" ]); // works fine

Pretty annoying, as in pure ES6 it works perfectly :(.

@FremyCompany
Copy link

I had the same issue when trying to wrap console.log().

log = function(...args) { console.log(...args) } // typing error here :(

for reference console.log is defined as

Console.log(message?: any, ...optionalParams: any[])

So my code is correct from the type point of view.

@mhegazy mhegazy added the Question An issue which isn't directly actionable in code label Feb 28, 2017
@mhegazy mhegazy closed this as completed Apr 19, 2017
@FremyCompany
Copy link

How is that a question? Perfectly valid ES6 code throws an error in TypeScript. That's a bug, no a feature.

@mhegazy
Copy link
Contributor

mhegazy commented Apr 19, 2017

your correct, relabeling. the issue is tracked by #5296

@mhegazy mhegazy added Duplicate An existing issue was already created and removed Question An issue which isn't directly actionable in code labels Apr 19, 2017
@FremyCompany
Copy link

👍

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants