简体   繁体   English

TypeScript 中解构赋值的语法错误

[英]Syntax Error on Destructuring Assignment in 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']]
}

Playground Link 游乐场链接

The above seems nice, and I think my assignment has no problem.以上看起来不错,我认为我的作业没有问题。

But It says: Type 'string[]' cannot be used as an index type.(2538)但它说: Type 'string[]' cannot be used as an index type.(2538)

I tried destructuring some times before and now have no idea about what's wrong.我之前尝试过几次解构,现在不知道出了什么问题。

Is it a TypeScript problem or a wrong syntax?是 TypeScript 问题还是语法错误? And how can I make it operate well?我怎样才能让它运作良好? Thanks a lot.非常感谢。

You need a semi colon after the call to super, or it's trying to run both statements together.在调用 super 之后需要一个分号,或者它试图同时运行两个语句。

The code, as it stands, reads as an index accessor to the return value from super, instead of a new statement.就目前而言,代码读取为 super 的返回值的索引访问器,而不是新语句。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM