javascript - 流 clearTimeout 与 TimeoutID 不兼容

标签 javascript react-native flowtype

我正在开发 React Native 应用程序,我需要使用 Flow。

我仍然在绕过 let arr: Array<React$Element<Component>> = [] 的整个概念

但就我的研究而言,有些东西我无法弄清楚如何定义我将用于 clearTimeout 的变量类型。

使用这个人所说的:0.63.x regression: setTimeout() & setInterval() return types broken

他说使用:

const foo: TimeoutID = setTimeout(() => {}, 300);
const bar: IntervalID = setInterval(() => {}, 300);

但最终我必须制作一个 clearTimeout()我得到:

Cannot call clearTimeout with this.playerTimeOut bound to timeoutId because null or undefined (see line 17) is incompatible with TimeoutID (see https://github.com/facebook/flow/blob/v0.78.0/lib/core.js#L733).

代码:

playerTimeOut: ?TimeoutID = null;

componentDidMount() {
  const { children } = this.props;
  const { index } = this.state;

  this.playerTimeOut = setTimeout(() => {
    clearTimeout(this.playerTimeOut);

    this.setState({
      index: index >= this.total ? 0 : index + 1
    });
  }, parseInt(children[index].attr.runningtime) * 1000);
}

非常感谢任何帮助或建设性的改进 :D

最佳答案

不确定您正在实现什么魔法,但会注意到超时将在第一次运行时被清除,因此您不需要显式调用 clearTimeout

但是,如果你确定你需要这个,那么流程看到的是:

playerTimeOut: ?TimeoutID = null;

因此,它可能为 null 或未定义。

要解决此问题:

a) 不定义maybe type :

playerTimeOut: TimeoutID; // no question mark, no default value.

Try

b) refine maybe type使用 if 条件:

if(this.playerTimeOut) {
   clearTimeout(this.playerTimeOut);
}

关于javascript - 流 clearTimeout 与 TimeoutID 不兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53299372/

相关文章:

react-native - 世博会独立应用程序: notification icons don't show up show up

javascript - 使用 CommonJS 的流声明文件

react-native - 无法在原子中生成命令

javascript - d3.js - 返回不存在元素的selectAll的目的是什么

javascript - 只能为 JSX 属性分配一个非空表达式

ios - 针对网络连接变化的设备的应用程序分析

reactjs - 为什么 Flow 将我的 React 组件报告为 `incompatible with string` ?

javascript - 使用 Iscroll 脚本设置滚动位置

javascript - 让 Google Bot 检测 AJAX 内容

javascript - 访问引号内的函数