我试图在typescript中连接两个字符串,如下所示:
//This array is declared as a class variable
testArray: String[] = ['first', second', 'third'];
concatArray(): void {
var testString = new String("");
this.testArray.forEach((item, index) => {
testString = testString.concat(item);
});
console.log(testString);
}
运行时,console.log将输出正确的值:' firstsecondthird'
但是我得到一个编译错误,上面写着:
error TS2345: Argument of type 'String' is not assignable to parameter of type 'string'.
'string' is a primitive, but 'String' is a wrapper object.
Prefer using 'string' when possible.
我不理解这个错误,因为testArray和testString似乎都是String类型的?
我是打字稿的新手。有人可以指出这里有什么问题吗?