亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

TypeScript用接口如何描述數組的問題

TypeScript用接口如何描述數組的問題

阿波羅的戰車 2018-11-14 14:13:49
interface Squares {  squares: (null | string)[]}interface History {  [index: number]: Squares}interface State {  history: History  stepNumber: number  xIsNext: Boolean}class Game extends React.Component {  state: State  constructor() {    super()    this.state = {      history: [{        squares: Array(9).fill(null)      }],      stepNumber: 0,      xIsNext: true    }  }    handleClick(i: number) {    const history = this.state.history.slice(0, this.state.stepNumber + 1)  }以上代碼為項目代碼的一部分,項目使用React+TypeScript開發,上面的代碼在vscode中提示錯誤:Property 'slice' does not exist on type 'History'.。slice是數組方法,如果換成類似let a: string[] = ['Hello']這種方式則slice方法可以正常使用不會報錯。題主目前是還是TypeScript初學者,想問一下各位:這種問題產生的原因是什么類似this.state這種結構的數據應該怎么用interface描述(主要是history這個數組怎么描述)
查看完整描述

1 回答

?
神不在的星期二

TA貢獻1963條經驗 獲得超6個贊

  1. 原因就是接口沒有正確繼承數組接口,導致數組的slice方法定義丟失

  2. 改成下面這樣

interface History extends Array<Squares>{}


查看完整回答
反對 回復 2018-12-29
  • 1 回答
  • 0 關注
  • 1987 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號