Skip to main content

错误抑制

Flow 针对许多常见的编程错误报告许多不同类型的错误,但并非所有 JavaScript 模式都能被 Flow 理解。如果你确信你的代码是正确的,并且 Flow 错误过于保守,你可以抑制该错误,以便 Flow 不会报告该错误。

¥Flow reports many different kinds of errors for many common programming mistakes, but not every JavaScript pattern can be understood by Flow. If you are confident your code is correct, and that Flow is erroring too conservatively, you can suppress the error so that Flow does not report it.

什么是抑制?

¥What is a Suppression?

抑制是一种特殊的注释,你可以将其放在类型错误之前的行上。它告诉 Flow 在检查代码时不要报告该错误。抑制注释如下所示:

¥A suppression is a special kind of comment that you can place on the line before a type error. It tells Flow not to report that error when checking your code. Suppression comments look like the following:

// <SUPPRESSOR>[<CODE>] extra text

抑制器可以是以下之一:

¥A suppressor can be one of the following:

  • $FlowFixMe:对于你打算稍后修复的类型错误

    ¥$FlowFixMe: for type errors that you intend to fix later

  • $FlowIssue:对于你怀疑是 Flow 问题的类型错误

    ¥$FlowIssue: for a type error that you suspect is an issue with Flow

  • $FlowExpectedError:对于你期望 Flow 产生类型错误的位置(例如,在执行无效类型转换时)。

    ¥$FlowExpectedError: for a location where you expect Flow to produce a type error (for instance, when performing an invalid type cast).

  • $FlowIgnore:对于你希望 Flow 忽略代码的位置

    ¥$FlowIgnore: for locations where you want Flow to ignore your code

请注意,所有抑制器的行为都相同;我们只是建议按照此处所述使用它们,以便你自己参考。

¥Note that all of the suppressors behave identically; we simply recommend using them as described here for your own ease of reference.

抑制的 <CODE> 部分是可选的,但当包含时指定抑制影响哪个 错误代码

¥The <CODE> portion of a suppression is optional, but when included specifies which error code the suppression affects.

压制注释的一些例子:

¥Some examples of suppression comments:

// $FlowFixMe

// $FlowIssue[incompatible-type]

/* $FlowIgnore[prop-missing] some other text here */

/* $FlowFixMe[incompatible-cast] this
is a multi-line
comment */

{ /* $FlowIssue this is how you suppress errors inside JSX */ }

为了成为有效的压制注释,还必须满足一些条件:

¥In order to be a valid suppression comment, there are also some conditions that must be true:

  • 抑制器之前或抑制器与代码之间不能有任何文本。例如:// some text then $FlowFixMe 不是有效抑制,// $FlowIssue some text [incompatible-type] //$FlowFixMe [prop-missing] 也不是有效抑制(注意此处的空格!)。

    ¥No text can precede the suppressor, or come between the suppressor and the code. For example: // some text then $FlowFixMe is not a valid suppression, nor is // $FlowIssue some text [incompatible-type] or //$FlowFixMe [prop-missing] (note the space here!).

  • 抑制必须在它们抑制的错误之前立即上线,否则它们将不适用。

    ¥Suppressions must be on the line immediately before the error they suppress, otherwise they will not apply.

使用错误代码使抑制更加精细

¥Making Suppressions More Granular with Error Codes

可抑制 Flow 错误也将有一个与之关联的错误代码(0.127 版本之后)。此代码简洁地描述了错误报告的问题类型,并且不同类型的错误之间有所不同。

¥Suppressible Flow errors will also have an error code associated with them (after version 0.127). This code concisely describes the type of issue the error is reporting, and is different between different kinds of errors.

为了防止抑制在同一行上抑制不同类型的错误(默认情况下,不带代码的抑制会抑制下一行上的每个错误),你可以在抑制中添加错误代码。例如:// $FlowFixMe[incompatible-cast] 只会抑制 incompatible-cast 代码的错误。所以:

¥In order to prevent suppressions from suppressing different kinds of type errors on the same line (by default suppressions without codes suppress every error on the following line), you can add an error code to your suppression. For example: // $FlowFixMe[incompatible-cast] would only suppress errors with the incompatible-cast code. So:

1// $FlowFixMe[incompatible-cast]23 as string;

不会报告任何错误,但是

¥would report no errors, but

1// $FlowFixMe[prop-missing]23 as string;

仍然会报告类型不兼容。

¥would still report a type incompatibility.

要抑制同一行上的多个错误代码,你可以逐个堆叠抑制注释,它们将全部应用于第一个非注释行,如下所示:

¥To suppress multiple error codes on the same line, you can stack suppression comments one after another, and they will all apply to the first non-comment line like so:

1let y: number | {x : number}  = 1;2
3// $FlowFixMe[incompatible-cast]4// $FlowFixMe[prop-missing]5y.x as string;

这将抑制该行上的两个错误。

¥This will suppress both of the two errors on this line.