错误 TS2356 表示数字类型变量必须是“数字”类型的算术操作数

chr*_*ris 0 javascript setinterval typescript

这是我的类定义的样子:

timeLeft: Number;

constructor() { 
    this.timeLeft = 60;
  }

  start() {
    console.log(typeof(this.timeLeft)); // prints out 'number'

    setInterval(() => {

      this.timeLeft--; // error TS2356 occurs here
      console.log(this.timeLeft);

    }, 1000);
  }

  ngOnInit() {}
Run Code Online (Sandbox Code Playgroud)

安慰:

错误 TS2356:算术操作数的类型必须为“any”、“number”或枚举类型。

Kri*_*han 5

正如 Phil 在评论部分提到的,最好使用“数字”而不是“数字”。“数字”指的是几乎从未在 JavaScript 代码中正确使用的非原始装箱对象。参考:做与不做