访问计算属性/元素。在HTMLElement中找不到可索引签名

时间:2016-09-29 13:59:15

标签: flowtype

我正在使用此代码段来获取视口的大小

function getViewport(): { width: number, height: number } {
  let e = window;
  let a = 'inner';

  if (!('innerWidth' in window)) {
    a = 'client';
    e = document.documentElement || document.body;
  }

  return {
    width: e[`${a}Width`],
    height: e[`${a}Height`],
  };
}

但是当我运行flow check时,我收到此错误:

 18:     height: e[`${a}Height`],
                 ^^^^^^^^^^^^^^^ access of computed property/element. Indexable signature not found in
 18:     height: e[`${a}Height`],
                 ^ HTMLElement

我刚刚开始学习流程,所以如果有人能解释我怎么能解决这个问题,或者当我能够阅读更多关于它的文章时,我会非常感激。谢谢。

1 个答案:

答案 0 :(得分:2)

取决于您想要做什么,但更简单的解决方法是:

let e: Object = window;