如何避免空数组的流输入错误?

How to avoid Flow typing error with empty array?

我遇到了一个我认为是漏报的流程错误。这是我的相关代码:

export type ValidationError = {
  name: string,
  id?: number,
  message: string
}

const determineErrorMessage = (name: string) => {
  if (!_.isEmpty(validationErrors)) {
    const error: ValidationError = validationErrors.find(error => error.name === name);

    if (error) {
      return (
        <Form.Label className={classes.errorMessage}>
          {error.message}
        </Form.Label>      
      );
    } else {
      return null;
    }
  } else {
    return null;
  }
}

这部分代码有错误:

validationErrors.find(error => error.name === name)

Flow 是这样说的:

Cannot assign `validationErrors.find(...)` to `error` because  undefined [1] is incompatible with  `ValidationError` 

我已经阅读了一些相关内容,甚至查阅了相关的 Flow 文档页面:https://flow.org/en/docs/types/arrays/ 在该页面的大约 2/3 处,它说 "As Flow is made to be smarter it may be possible in the future to fix this problem, but for now you should be aware of it."

我是否应该接受我没有做错任何事情并且 Flow 将来会得到改进?

Flow 在这里指出了一个合理的错误。

const error: ValidationError = 

应该是

const error: ValidationError | void =

因为如果找不到任何东西,find 将 return undefined

但是,假设 validationErrors 已声明为 Array<ValidationError>,那么您不需要首先包含 : ValidationError,因为 Flow 知道给定 Array<T> , 然后 find returns T | void, 让你做

const error =