Skip to main content

Flow 严格

你可以通过将 @flow strict 添加到文件来在 Flow 中启用更强大的安全保证(例如禁止 any/Object/Function 类型并要求键入所有依赖)。

¥You can enable stronger safety guarantees in Flow (such as banning any/Object/Function types and requiring all dependencies to be typed) by adding @flow strict to your files.

概述

¥Overview

Flow 的设计是为了易于采用,因此它允许你在某些情况下选择退出类型检查,从而允许不安全的行为。但由于现在许多代码库都高度采用 Flow 类型,因此可以翻转这种权衡。你可以使用 Flow Strict 来禁止以前允许的不安全模式。这为你提供了更好的安全保证,可以捕获更多错误并使重构变得更容易。你可以逐个文件地逐步实现这些更强有力的保证。

¥Flow was designed for easy adoption, so it allows you opt-out of type checking in certain situations, permitting unsafe behaviors. But since many codebases now have a high adoption of Flow types, this trade-off can be flipped. You can use Flow Strict to disallow previously-allowed unsafe patterns. This gives you improved safety guarantees that catch more bugs and make refactoring easier. And you can implement these stronger guarantees incrementally, on a file-by-file basis.

特性

¥Features

为文件启用 Flow Strict 意味着以前允许的几种模式现在将触发 Flow 错误。每个不允许的模式都有一个相应的 Flow Lint 规则,该规则会触发错误。为 @flow strict 启用的规则列表在每个 .flowconfig 中配置。以下是推荐规则:

¥Enabling Flow Strict for a file means that several previously-allowed patterns will now trigger a Flow error. Each disallowed pattern has a corresponding Flow Lint rule which triggers the error. The list of rules enabled for @flow strict is configured in each .flowconfig. Here are the recommended rules:

  • nonstrict-import:从不是 @flow strict 的模块导入时触发错误。这非常重要,因为这意味着当一个文件被标记为严格时,它的所有依赖也都是严格的。

    ¥nonstrict-import: Triggers an error when importing from a module which is not also @flow strict. This is very important, because it means that when a file is marked as strict, all of its dependencies are strict as well.

  • unclear-type:在类型注释中使用 ObjectFunctionany 时触发错误。

    ¥unclear-type: Triggers an error when using Object, Function, or any in a type annotation.

  • untyped-import:从无类型模块导入时触发错误。

    ¥untyped-import: Triggers an error when importing from an untyped module.

  • untyped-type-import:从无类型模块导入类型时触发错误。

    ¥untyped-type-import: Triggers an error when importing a type from an untyped module.

  • unsafe-getters-setters:使用 getter 和 setter 时会触发错误,这可能是不安全的。

    ¥unsafe-getters-setters: Triggers an error when using getters and setters, which can be unsafe.

  • sketchy-null:对可能为 null/未定义或 false 的值进行存在性检查时触发错误。

    ¥sketchy-null: Triggers an error when doing an existence check on a value that could be null/undefined or falsey.

有关可用 lint 规则的完整列表,请参阅 Lint 规则参考

¥For a full list of available lint rules, see the Lint Rule Reference.

另外,请注意,函数参数被视为 const(即,视为使用 const 而不是 let 声明它们)。此功能在 Flow Strict 中尚不可配置;它始终开启。

¥Additionally, note that function parameters are considered const (i.e., treated as if they were declared with const rather than let). This feature is not yet configurable in Flow Strict; it is always on.

在 .flowconfig 中启用 Flow Strict

¥Enabling Flow Strict in a .flowconfig

Flow Strict 在每个 .flowconfig 中配置。启用:

¥Flow Strict is configured in each .flowconfig. To enable:

  1. [strict] 部分添加到 .flowconfig

    ¥Add a [strict] section to the .flowconfig.

  2. 列出要启用的 lint 规则。 强烈推荐这些:

    ¥List the lint rules to enable . These are strongly recommended:

[strict]
nonstrict-import
unclear-type
unsafe-getters-setters
untyped-import
untyped-type-import

也推荐,但可选,因为它在某些代码库中可能太嘈杂:sketchy-null

¥Also recommended, but optional as it may be too noisy in some codebases: sketchy-null

我们建议你从一开始就启用所有所需的规则,然后逐个文件采用 Flow Strict。这比启用单个规则、将 @flow strict 添加到许多文件,然后向配置添加更多规则效果更好。

¥We recommend you enable all your desired rules from the beginning, then adopt Flow Strict file-by-file. This works better than enabling a single rule, adding @flow strict to many files, and then adding more rules to the config.

采纳

¥Adoption

@flow strict 添加到文件并修复出现的所有错误。由于 Flow Strict 要求依赖也很严格(如果启用了 nonstrict-import 规则),因此从依赖树的叶子开始并从那里开始工作。不要添加 $FlowFixMe 来抑制出现的新错误;所有问题都解决后,只需添加 @flow strict 即可。由于使用 $FlowFixMe 的最常见原因源于对无类型依赖或行为的依赖,因此一旦启用 Flow Strict,未来的问题应该会大大减少。

¥Add @flow strict to a file and fix all errors that appear. Because Flow Strict requires dependencies to also be strict (if the nonstrict-import rule is enabled), start at the leaves of the dependency tree and work up from there. Do not add $FlowFixMe to suppress the new errors as they appear; just add @flow strict once all issues have been resolved. Since the most common reasons for using $FlowFixMe stem from reliance on untyped dependencies or behavior, future issues should be greatly reduced once Flow Strict is enabled.

自由地启用 Flow Strict。与添加或删除 @flow 不同,添加或删除 @flow strict(单独)不会更改 Flow 覆盖范围。它仅阻止或允许将来添加某些新的不安全行为。即使将来必须对文件禁用 Flow Strict,至少可以防止同时添加不安全行为。

¥Be liberal with enabling Flow Strict. Unlike adding or removing @flow, adding or removing @flow strict (by itself) does not change Flow coverage. It only prevents or allows certain new unsafe behavior from being added in the future. Even if in the future Flow Strict has to be disabled for the file, at least unsafe behavior was prevented from being added in the meantime.

库定义被认为是严格的(因为它们可以包含在具有矛盾的严格配置的许多不同项目中)。

¥Library definitions are considered strict (as they can be included in many different projects with contradicting strict configurations).

严格本地

¥Strict Local

如果你在 Flow Strict 配置中启用 nonstrict-import 规则(推荐),则严格文件的所有依赖也必须是严格的。虽然这是最佳目标,但对于大型预先存在的代码库来说,在所有依赖都严格之前允许使用 Flow Strict 的一些好处可能是有益的。

¥If you enable the nonstrict-import rule in your Flow Strict configuration (recommended), then all dependencies of a strict file must also be strict. While this the optimal goal, for large pre-existing codebases it may be beneficial to allow some of the benefits of Flow Strict to be put in use before all dependencies are strict.

@flow strict-local@flow strict 相同,只是它不要求其依赖也很严格(即,它是 "本地" 严格的)。它没有单独的配置:它使用与 Flow Strict 相同的配置,只是没有 nonstrict-import 规则。

¥@flow strict-local is the same as @flow strict, except it does not require its dependencies to also be strict (i.e. it is "locally" strict). It does not have a separate configuration: it uses the same configuration as Flow Strict, just without the nonstrict-import rule.

一旦 @flow strict-local 文件的所有依赖都严格,该文件就可以升级为 @flow strict 文件。@flow strict 文件不能依赖于 @flow strict-local 文件,因为这会违反 nonstrict-import 规则。

¥Once all the dependencies of a @flow strict-local file are strict, the file can be upgraded to a @flow strict file. A @flow strict file cannot depend on a @flow strict-local file as this would break the nonstrict-import rule.

未来是什么

¥What's Ahead

最终,Flow Strict 的某些功能可能会成为 Flow 的默认行为,如果这些功能被证明是成功的并获得广泛采用。

¥Eventually, some features of Flow Strict could become the default behavior of Flow, if those features prove successful and achieve widespread adoption.