TypeScript中构造赋值时的语法错误


interface User extends Function {
player: number,
units: number[],
sites: string[],
}
class User extends Function {
constructor() {
super('return this.player')
[this.player, this.units, this.sites] = getBelongings(); // Destructuring Assignment

}
}
const me = new User();
function getBelongings(): [number, number[], string[]] {
return [1, [1], ['1']]
}

操场上联系

以上看起来不错,我认为我的作业没有问题。

但是上面写着:Type 'string[]' cannot be used as an index type.(2538)

我以前试过几次解构,现在不知道是什么问题。

是TypeScript问题还是语法错误?我怎样才能使它运转良好呢?非常感谢。

在调用super之后需要一个分号,否则它会尝试同时运行两个语句。

这段代码实际上是作为一个索引访问器读取super的返回值,而不是一个新语句。

super('return this.player');   // <-- insert semi colon
[this.player, this.units, this.sites] = getBelongings(); 

最新更新