代码之家  ›  专栏  ›  技术社区  ›  chris

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

  •  0
  • chris  · 技术社区  · 7 年前

    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() {}
    

    慰问:

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

    1 回复  |  直到 7 年前
        1
  •  5
  •   Krishna Mohan    7 年前

    正如菲尔在评论部分提到的,最好用“number”而不是“number”“数字”指的是在JavaScript代码中几乎从未正确使用过的非基本装箱对象。 Do's and Don'ts