3

Using VS17RC with TS 2.1

function foo(x:number, y:number, z:number) { console.log(x + y + z)}
var args = [0, 1, 2];
foo(...args);

gives the compile time error "Supplied parameters do not match any signature of call target."

This is the compiled js:

function foo(x, y, z) { console.log(x + y + z); }
var args = [0, 1, 2];
foo.apply(void 0, args);

which does in fact work.

Is there something I am doing wrong here?

The example comes from here: Typescript Deep Dive

2 Answers 2

3

There's an open issue on the exact same example:
Compiler incorrectly reports parameter/call target signature mismatch when using the spread operator

The issue is marked as bug, but it's there from Aug 3, 2015, and has no set milestone.

Sign up to request clarification or add additional context in comments.

1 Comment

Well, that explains it for now.
1

This is my workaround for it:

/**
 * Function definition
 */
class SockService {
  connect(...args:Array<string>) {
    if (args.length > 1) {
      [this.host, this.path] = args;
    }
  }
}

/**
 * Function usage
 */
var endpoint = {
  'local_test': [] as Array<string>,
  'local': ['0.0.0.0:8080', '/foo/websocket'],
  'production': ['192.0.2.1:8080', '/foo/websocket']
};

this.sock.connect(...endpoint.local_test);

I don't like using rest parameters like this, as it makes the function definition much less descriptive. But it's the only way I've gotten spread to work with function arguments in TypeScript.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.