[英]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']]
}
以上看起來不錯,我認為我的作業沒有問題。
但它說: 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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.