Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing array to typescript rest parameter

Tags:

typescript

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

like image 864
Ľuboš Pilka Avatar asked Dec 31 '25 18:12

Ľuboš Pilka


1 Answers

Use the typescript spread operator:

let foo = new Foo(...restParamValues);
like image 186
Duncan Avatar answered Jan 03 '26 10:01

Duncan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!