Reputation: 1148
Is there a way how to pass array of arguments into typescript rest parameter, without changing implementation of Foo class ?
class Foo{
constructor(...restParam : string[]){}
}
class Test{
CallFoo = () => {
// Working
let foo = new Foo("t", "t");
// Compilation error
let restParamValues = ["t", "t"];
let foo2 = new Foo(restParamValues);
};
Error: Argument of type 'string[]' is not assignable to parameter of type 'string'.
Upvotes: 3
Views: 1362
Reputation: 95642
Use the typescript spread operator:
let foo = new Foo(...restParamValues);
Upvotes: 4