我正在开发 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
withthis.playerTimeOut
bound totimeoutId
because null or undefined (see line 17) is incompatible withTimeoutID
(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.
b) refine maybe type使用 if
条件:
if(this.playerTimeOut) {
clearTimeout(this.playerTimeOut);
}
关于javascript - 流 clearTimeout 与 TimeoutID 不兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53299372/