Javascript FLOW:如何使流与Array.prototype.find()的回调一起工作

Javascript FLOW:如何使流与Array.prototype.find()的回调一起工作,javascript,ecmascript-6,flowtype,flow-typed,Javascript,Ecmascript 6,Flowtype,Flow Typed,伙计们,我是新手 我有这个密码 type importItem = { name: string, groupRank: number, rank: number, node: Object, }; function findTargetImportItem(importedItems: Array<importItem>, outOfOrderItem: importItem) : importItem { return importedItems.find((i

伙计们,我是新手

我有这个密码

type importItem = {
  name: string,
  groupRank: number,
  rank: number,
  node: Object,
};
function findTargetImportItem(importedItems: Array<importItem>, outOfOrderItem: importItem) : importItem {

return importedItems.find((importedItem: importItem) => importedItem.rank > outOfOrderItem.rank);}
类型importItem={
名称:string,
groupRank:number,
职级:编号,,
节点:对象,
};
函数findTargetImportItem(导入项:数组,outOfOrderItem:importItem):importItem{
返回importedItems.find((importedItem:importItem)=>importedItem.rank>outOfOrderItem.rank);}
我犯了这个错误

Cannot return importedItems.find(...) because undefined [1] is incompatible with importItem [2].

     src/rules/import-order.js
 [2]  74│ function findTargetImportItem(importedItems: Array<importItem>, outOfOrderItem: importItem) : importItem {
      75│   /**
      76│      * Return the import where the unordered imports will be moving towards
      77│      */
      78│   return importedItems.find((importedItem: importItem) => importedItem.rank > outOfOrderItem.rank);
      79│ }
      80│
      81│ function hasTrailingSpace(sourceCode, node) {

     /private/tmp/flow/flowlib_21840530/core.js
 [1] 244│     find(callbackfn: (value: T, index: number, array: Array<T>) => any, thisArg?: any): T | void;
无法返回importedItems.find(…),因为未定义的[1]与importItem[2]不兼容。
src/rules/import-order.js
[2]  74│ 函数findTargetImportItem(导入项:数组,outOfOrderItem:importItem):importItem{
75│   /**
76│      * 返回无序导入将移动到的导入
77│      */
78│   返回importedItems.find((importedItem:importItem)=>importedItem.rank>outOfOrderItem.rank);
79│ }
80│
81│ 函数hasTrailingSpace(源代码,节点){
/private/tmp/flow/flowlib_21840530/core.js
[1] 244│     find(callbackfn:(值:T,索引:number,数组:array)=>any,thisArg?:any):T | void;
我不知道如何让Flow知道find helper函数返回的内容是importItem类型


你们能帮我解决这个问题吗?

流编译器是正确的。它知道
find()
返回的值可以是
未定义的

如果数组中的任何项都不满足您传递的回调中的条件,则返回值将为
undefined
。请将
findTargetImportItem()
的返回类型更改为
void | importItem
或指定
find()的返回值
到一个临时变量,如果临时变量
未定义,则返回一些类型为
importItem
的默认值

解决方案1
函数findTargetItem(导入项:数组,outOfOrderItem:importItem):void | importItem{
/**
*返回无序导入将移动到的导入
*/
返回importedItems.find((importedItem:importItem)=>importedItem.rank>outOfOrderItem.rank);
}
解决方案2
constdefaultimportitem:importItem=。。。;
函数findTargetImportItem(导入项:数组,outOfOrderItem:importItem):importItem{
/**
*返回无序导入将移动到的导入
*/
const importedItem=importedItems.find((importedItem:importItem)=>importedItem.rank>outOfOrderItem.rank);
return importedItem==未定义
?默认进口税
:进口物品;
}

或针对缺少的值显式抛出异常。感谢您的回答!但是我尝试了您的第一个解决方案,但我遇到了此错误。
无法使用undefined作为类型,因为undefined是一个值。要获取值的类型,请使用typeof。
@ZhenghaoHe try
void
。很抱歉,我不知道flow不允许
undefinedned
作为类型(因为TypeScript有)。谢谢!@PatrickRoberts。我还可以问一下,对于所有使用流类型的JS文件,我们需要使用一些编译器(如Babel)去除流类型,以便它能够正常工作,从而使这些类型注释只在开发过程中出现。这是否意味着我们永远不知道这些类型注释是如何用于JS的项目,例如ESLint插件,因为我们可以看到的代码已经被剥离?完全同意,它100%取决于没有找到的项目是否符合该文章中的“骨头头”或“致命”的条件。
function findTargetImportItem(importedItems: Array<importItem>, outOfOrderItem: importItem) : void | importItem {
  /**
    * Return the import where the unordered imports will be moving towards
    */
  return importedItems.find((importedItem: importItem) => importedItem.rank > outOfOrderItem.rank);
}
const defaultImportItem: importItem = ...;

function findTargetImportItem(importedItems: Array<importItem>, outOfOrderItem: importItem) : importItem {
  /**
    * Return the import where the unordered imports will be moving towards
    */
  const importedItem = importedItems.find((importedItem: importItem) => importedItem.rank > outOfOrderItem.rank);

  return importedItem === undefined
    ? defaultImportItem
    : importedItem;
}