有没有办法如何将参数数组传递给typescript rest参数,不用更改Foo类的实现?
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);
};
错误:类型'string []'的参数不能分配给'string'类型的参数。
答案 0 :(得分:2)
使用typescript spread operator:
let foo = new Foo(...restParamValues);