3

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 Answer 1

4

Use the typescript spread operator:

let foo = new Foo(...restParamValues);
Sign up to request clarification or add additional context in comments.

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.