Passing array to typescript rest parameter

Viewed 1043

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'.

1 Answers

Use the typescript spread operator:

let foo = new Foo(...restParamValues);
Related