我是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 helper函数返回的内容是importItem类型。
你们能帮我吗
答案 0 :(得分:4)
流编译器是正确的。它知道<div class="container">
</div>
返回的值可以为find()
。
如果数组中的所有项目都不满足您传递的回调中的条件,则返回值为undefined
。将undefined
的返回类型更改为findTargetImportItem()
,或者将void | importItem
的返回值分配给临时变量,如果临时变量为{,则返回类型为find()
的默认值{1}}。
importItem
undefined