【问题标题】:FLOW: how to make Flow work with callback for Array.prototype.find()FLOW:如何使 Flow 与 Array.prototype.find() 的回调一起工作
【发布时间】:2019-03-31 16:08:37
【问题描述】:

伙计们,我是 Flow 的新手。

我有这个代码

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);}

我收到了这个错误

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;

我不知道如何让 Flow 知道 find 辅助函数返回的东西是 importItem 类型。

你们能帮帮我吗

【问题讨论】:

    标签: javascript ecmascript-6 flowtype flow-typed


    【解决方案1】:

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

    如果数组中没有任何项满足您传递的回调中的条件,则返回值为undefined。将findTargetImportItem() 的返回类型更改为void | importItem,或者将find() 的返回值分配给临时变量,如果临时变量为undefined,则返回importItem 类型的一些默认值。

    解决方案 1

    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);
    }
    

    解决方案 2

    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;
    }
    

    【讨论】:

    • 或者明确抛出关于缺失值的异常。
    • 感谢您的回答!但是,我尝试了您的第一个解决方案,但出现了此错误。 Cannot use undefined as a type because undefined is a value. To get the type of a value use typeof..
    • @ZhenghaoHe 试试void。抱歉,我不知道 flow 不允许 undefined 作为类型(因为 TypeScript 允许)。
    • 谢谢! @帕特里克罗伯茨。我也可以问一下,是不是所有使用流类型的JS文件。我们需要使用 Babel 之类的编译器剥离 Flow 类型,以便它能够正常工作,以便这些类型注释仅在开发过程中出现。这是否意味着我们永远不知道这些类型注释如何用于 JS 项目,例如ESLint 插件是因为我们可以看到的代码已经被剥离了?
    • 完全同意这 100% 取决于未找到的项目在该文章的条款中是否符合“愚蠢”或“致命”的条件。
    猜你喜欢
    • 2017-02-27
    • 2018-10-09
    • 1970-01-01
    • 2020-12-22
    • 1970-01-01
    • 1970-01-01
    • 2018-05-27
    • 2012-08-13
    • 2020-01-08
    相关资源
    最近更新 更多