Ľuboš Pilka
Ľuboš Pilka

Reputation: 1148

Passing array to typescript rest parameter

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

Answers (1)

Duncan
Duncan

Reputation: 95642

Use the typescript spread operator:

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

Upvotes: 4

Related Questions