【问题标题】:error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type错误 TS2362:算术运算的左侧必须是“any”、“number”、“bigint”类型或枚举类型
【发布时间】:2019-10-18 18:04:55
【问题描述】:

在我的 Angular 8 应用程序中,我收到错误错误 TS2362:算术运算的左侧必须是“任意”、“数字”、“大整数”或枚举类型。在下面的代码行中

常量源:any = timer(0, environment.corePingIntervalSeconds * 1000);

const source: any = timer(0, environment.corePingIntervalSeconds * 1000);
    source.subscribe(() => {
       this.checkIfCoreApiIsAvailable()
       .pipe(first())
       .subscribe(resp  => {

       }, err => console.log(err));
     });

【问题讨论】:

  • environment.corePingIntervalSeconds的值/类型是什么?

标签: angular


【解决方案1】:

你应该尝试使用 Number 接口:

timer(0, Number(environment.corePingIntervalSeconds) * 1000);

【讨论】:

    【解决方案2】:

    你可以使用

     +timer(0, environment.corePingIntervalSeconds * 1000)
    

    前面的加号将其转换为数字类型

    【讨论】:

      【解决方案3】:

      强调文本算术运算符的左侧值必须是 number, any, bigint, or enum 类型。在您的情况下,environment.corePingIntervalSeconds 的值不属于这些类型。在这种情况下,您应该使用显式类型转换。

      您的正确语法如下所示

      const source: any = timer(0, (environment.corePingIntervalSeconds as any) * 1000);
      

      【讨论】:

        猜你喜欢
        • 2020-12-30
        • 1970-01-01
        • 2016-08-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-24
        相关资源
        最近更新 更多