小编Jos*_*ggs的帖子

流 clearTimeout 与 TimeoutID 不兼容

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

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

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

使用这家伙所说的:0.63.x 回归:setTimeout() 和 setInterval() 返回类型已损坏

他说使用:

const foo: TimeoutID = setTimeout(() => {}, 300);
const bar: IntervalID = setInterval(() => {}, 300);
Run Code Online (Sandbox Code Playgroud)

但最终我必须做一个 clearTimeout(),我得到:

不能打电话clearTimeoutthis.playerTimeOut必然timeoutId,因为null或undefined(见第17行)是不兼容的TimeoutID(见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 >= …
Run Code Online (Sandbox Code Playgroud)

javascript flowtype react-native

3
推荐指数
1
解决办法
1851
查看次数

流类型属性“ msg”缺少null或未定义

我发现很难使用Flow。我知道Array.find可以返回或不确定。

因此,通过阅读以下内容:github:Array <{...}>上的Array.find引发

我做到了,并添加void到类型。但是现在我得到了错误:

无法获取,stageMsg.msg因为msg缺少属性为null或未定义(请参见第92行)。

检查代码以查看错误的具体位置。

我知道为什么会给我错误,没有msg未定义的错误。我不知道该如何解决这个问题。

这是代码:

type StageMsg = {
  stage: number,
  msg?: string
};

let stageMsg: void | StageMsg = this.stages.find(
  (obj: StageMsg) => obj.stage === this.state.stage
);

msg = (
  <Msg text={this.state.msg !== "" ? this.state.msg : stageMsg.msg} /> //<---- [Error here].
);
Run Code Online (Sandbox Code Playgroud)

谢谢先进的人:D

javascript flowtype react-native

0
推荐指数
1
解决办法
1238
查看次数

标签 统计

flowtype ×2

javascript ×2

react-native ×2