【问题标题】:The right-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type算术运算的右侧必须是“any”、“number”、“bigint”类型或枚举类型
【发布时间】:2020-02-28 05:58:34
【问题描述】:

我在 JS 中有这段代码,我需要让它在 TypeScript 中工作。

categoryAxis.renderer.labels.template.adapter.add("dy", function(dy, target) {
      if (target.dataItem && target.dataItem.index & 2 == 2) {
        return dy + 25;
      }
      return dy;
    });

在这个2 == 2 上,我遇到了错误。

【问题讨论】:

    标签: angular typescript amcharts


    【解决方案1】:

    如果这确实是您拥有的确切 javascript:

    target.dataItem && target.dataItem.index & 2 == 2
    

    那么就和

    一样
    target.dataItem && target.dataItem.index & (2 == 2)
    target.dataItem && target.dataItem.index & true
    

    true 将被 javascript 自动强制转换为 1,但 TypeScirpt 不会隐式执行此操作。

    所以如果你写这个:

    target.dataItem && target.dataItem.index & 1
    

    target.dataItem && target.dataItem.index & +true
    

    在 TypeScript 中,它编译并执行与您上面的 javascript 相同的操作。

    否则您的 javascript 已经错了,这里的其他答案之一已经建议 xyou 进行各种更正。

    【讨论】:

      【解决方案2】:

      看起来打字稿首先执行2 == 2,它评估为true,然后是target.dataItem.index & true,这是错误的。尝试添加括号(target.dataItem.index & 2) == 2

      【讨论】:

      • @Pac0 对,也许只是target.dataItem.index & 1 ?
      • 也许,在 OP 确认之前,我们无法真正确定
      • (target.dataItem.index & 1) == 1 我试过了,效果很好。
      【解决方案3】:

      你的问题可能是target.dataItem.index & 2,我猜你的意思是双&:target.dataItem.index && 2

      【讨论】:

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