【问题标题】:Flow type - index signature declaring the expected key / value type is missing in property touches of unknown type流类型 - 未知类型的属性触摸中缺少声明预期键/值类型的索引签名
【发布时间】:2020-06-30 14:51:07
【问题描述】:

我在输入事件时遇到问题。如果我这样做:

function(e: MouseEvent | TouchEvent) {
   const coords = e.touches ? e.touches[0].clientX : e.clientX
}

我收到此错误

 1. Cannot get e.clientX because property clientX is missing in TouchEvent
 2. Cannot get e.touches[0] because an index signature declaring the expected key / value type is
missing in property touches of unknown type 

感谢您的帮助。

【问题讨论】:

    标签: javascript reactjs flowtype


    【解决方案1】:

    在流程中,检查对象类型的方法不是验证某个属性是否存在,因为这并不总是正确的。相反,您想使用instanceof

    我稍微改变了你的函数,让它返回,但它应该是你要找的。​​p>

    function getClientX(e: MouseEvent | TouchEvent): number {
      if (e instanceof TouchEvent) {
        return e.touches[0].clientX;
      }
      return e.clientX;
    }
    

    【讨论】:

      猜你喜欢
      • 2019-11-13
      • 2021-10-11
      • 2019-04-28
      • 2022-08-21
      • 1970-01-01
      • 2020-01-17
      • 2021-12-26
      • 2022-10-05
      • 2021-04-20
      相关资源
      最近更新 更多