流类型的定义被忽略以支持 npm 模块?

Flow-typed definitions being ignored in favor of npm module?

考虑以下 Javascript 和流程代码:

import type { $Request, $Response } from 'express';

function middleware(req: $Request, res: $Response) {}

middleware({}, {});

(完整代码位于 https://github.com/bradvogel/flow-playground

express 作为 npm 模块安装时,Flow 会正确标记代码错误:

但是,当我npm install express时,Flow 无法再解析类型(来自 flow-typed):

有人可以解释一下 Flow 试图从 Express 模块导入类型,还是从 flow-typed 导入类型?我该如何克服这个问题?

Flow 并不真正了解 Node 包作为一个单元,因此如果您不希望 Flow 尝试解析 node_modules 中的内容,您需要

[ignore]
<PROJECT_ROOT>/node_modules/.*

在你的 .flowconfig 中。如果您确实想要允许 node_modules 的一个子集,[ignore] 解释了如何做到这一点。

我不知道 Flow 如何优先考虑来自 flow-typed 和真实文件的 explicitly-declared 类型定义,但大概考虑到我们在这里看到的情况,Flow 必须尝试从实际导入的文件,除非该文件被忽略。