Skip to main content

.flowconfig [declarations]

通常,第三方库的类型定义已损坏,或者类型定义仅与特定版本的 Flow 兼容。在这些情况下,使用第三方库中的类型信息而不对其内容进行类型检查可能会很有用。

¥Often third-party libraries have broken type definitions or have type definitions only compatible with a certain version of Flow. In those cases it may be useful to use type information from the third-party libraries without typechecking their contents.

.flowconfig 文件中的 [declarations] 部分告诉 Flow 在声明模式下解析与指定正则表达式匹配的文件。在声明模式下,代码不进行类型检查。然而,类型检查器在检查其他代码时会提取并使用函数、类等的签名。

¥The [declarations] section in a .flowconfig file tells Flow to parse files matching the specified regular expressions in declaration mode. In declaration mode the code is not typechecked. However, the signatures of functions, classes, etc are extracted and used by the typechecker when checking other code.

从概念上讲,我们可以将声明模式想象为 Flow 仍然对文件进行类型检查,但行为就像每一行都有 $FlowFixMe 注释。

¥Conceptually one can think of declaration mode as if Flow still typechecks the files but acts as if there is a $FlowFixMe comment on every line.

另请参阅 [untyped],了解不对文件进行类型检查,而是对所有内容使用 any

¥See also [untyped] for not typechecking files, and instead using any for all contents.

要记住的事情:

¥Things to keep in mind:

  1. 声明模式只能用于现有的第三方代码。你永远不应该将其用于你控制下的代码。

    ¥Declaration mode should only be used for existing third-party code. You should never use this for code under your control.

  2. 这些是 OCaml 正则表达式

    ¥These are OCaml regular expressions.

  3. 这些正则表达式与绝对路径匹配。他们可能应该从 .* 开始

    ¥These regular expressions match against absolute paths. They probably should start with .*

[declarations] 部分的示例可能如下所示:

¥An example [declarations] section might look like:

[declarations]
.*/third_party/.*
.*/src/\(foo\|bar\)/.*
.*\.decl\.js

这个 [declarations] 部分将以声明模式解析:

¥This [declarations] section will parse in declaration mode:

  1. 名为 third_party 的目录下的任何文件或目录

    ¥Any file or directory under a directory named third_party

  2. .*/src/foo.*/src/bar 下的任何文件或目录

    ¥Any file or directory under .*/src/foo or under .*/src/bar

  3. 任何以扩展名 .decl.js 结尾的文件

    ¥Any file that ends with the extension .decl.js

你可以在正则表达式中使用 <PROJECT_ROOT> 占位符。在运行时,Flow 会将占位符视为项目根目录的绝对路径。这对于编写相对而不是绝对的正则表达式很有用。

¥You may use the <PROJECT_ROOT> placeholder in your regular expressions. At runtime, Flow will treat the placeholder as if it were the absolute path to the project's root directory. This is useful for writing regular expressions that are relative rather than absolute.

例如,你可以写:

¥For example, you can write:

[declarations]
<PROJECT_ROOT>/third_party/.*

它将在声明模式下解析项目根目录中名为 third_party/ 的目录下的任何文件或目录。但是,与前面示例的 .*/third_party/.* 不同,它不会解析名为 third_party/ 的目录下的文件或目录,例如 src/third_party/

¥Which would parse in declaration mode any file or directory under the directory named third_party/ within the project root. However, unlike the previous example's .*/third_party/.*, it would NOT parse files or directories under directories named third_party/, like src/third_party/.