TypeScript: Object is of type 'unknown'.

错误代码展示

image

解决方案

e声明为any类型,如下所示:

image

// 修改蛇的X和Y值
try {
  this.snake.X = X;
  this.snake.Y = Y;
}catch(e:any){
  // 进入到catch,说明出现了异常,游戏结束,弹出一个提示信息
  alert(e.message);
  // 将isLive设置为false
  this.isLive = false;
}
posted @ 2021-11-01 15:33  胸怀丶若谷  阅读(3002)  评论(1编辑  收藏  举报