Skip to main content

Flow 枚举

Flow 枚举定义了一组固定的常量,这些常量创建了自己的类型。

¥Flow Enums define a fixed set of constants which create their own type.

与 Flow 的其他功能不同,Flow Enum 在运行时以值的形式存在,也以类型的形式存在。

¥Unlike other features of Flow, Flow Enums exist as values at runtime, as well as existing as types.

了解如何在项目中启用 Flow Enums

¥Read how to enable Flow Enums in your project.

好处

¥Benefits

与现有模式相比,枚举具有以下几个优点:

¥Enums provide several benefits over existing patterns:

  • 减少重复:枚举声明提供枚举的类型和值。

    ¥Reduce repetition: Enum declarations provide both the type and the value of the enum.

  • 提高 Flow 性能:枚举保证具有良好的类型检查性能,这与联合体不同,联合体在某些情况下进行类型检查的成本可能很高。

    ¥Improve Flow performance: Enums are guaranteed to have good type-checking performance, unlike unions which may be expensive to type-check in certain situations.

  • 启用新功能:枚举带有 cast 方法,它可以安全地从原始类型转换为枚举类型。

    ¥Enable new functionality: Enums come with a cast method, which converts from a primitive type to an enum type safely.

  • 增强安全性:枚举定义了自己的类型,该类型不会隐式强制转换为其他类型(例如,来自 string),并且必须为 彻底检查 switch 语句。这些属性可以帮助防止逻辑错误。

    ¥Enhance safety: Enums define their own type which does not implicitly coerce to and from other types (e.g. from strings), and are required to be exhaustively checked in switch statements. These properties can help prevent logic bugs.

快速入门

¥Quickstart

定义枚举

¥Defining enums

名为 Status 的枚举,包含三个成员:ActivePausedOff

¥An enum named Status with three members: Active, Paused, and Off.

1enum Status {2  Active,3  Paused,4  Off,5}

默认情况下,枚举使用反映其名称的字符串值来定义成员。你还可以显式设置值:

¥By default, enums define members with string values which mirror their names. You can also explicitly set values:

1enum Status {2  Active = 'active',3  Paused = 'paused',4  Off = 'off',5}

你也可以使用数字:

¥You can use numbers as well:

1enum Status {2  Active = 1,3  Paused = 2,4  Off = 3,5}

值必须是唯一的、字面量的并且全部具有相同的类型。查看 有关定义枚举的完整文档 了解更多信息。

¥Values must be unique, literals, and all of the same type. Check out the full docs on defining enums to learn more.

使用枚举

¥Using enums

要访问枚举成员,请使用点访问:

¥To access an enum member, use dot access:

Status.Active

要将枚举类型用作注释,请使用枚举名称:

¥To use the enum type as an annotation, use the enum name:

const status: Status = Status.Active;

从表示类型(在本例中为 string)转换为枚举类型:

¥Cast from the representation type (in this case, a string) to the enum type:

const status: Status | void = Status.cast(someString);

你可以使用 ?? 运算符轻松提供默认值:

¥You can easily provide a default value with the ?? operator:

const status: Status = Status.cast(someString) ?? Status.Off;

了解有关 枚举提供的其他方法 的更多信息,包括 isValidmembersgetName

¥Read more about the other methods enums provide, including isValid, members, and getName.

将枚举类型转换为其表示类型(必须显式完成):

¥Cast an enum type to its representation type (must be done explicitly):

status as string

switch 语句中枚举的检查是详尽的 - 我们确保你检查所有成员:

¥Checks of enums in switch statements are exhaustive - we ensure you check all members:

1enum Status {2  Active,3  Paused,4  Off,5}6const status: Status = Status.Active;7
8// ERROR: Incomplete exhaustive check9switch (status) {10  case Status.Active:  break;11  case Status.Paused: break;12  // We forgot to add `case: Status.Off:` here, resulting in error above.13  // Using `default:` would also work to check all remaining members.14}

了解有关 详尽地检查枚举 的更多信息。

¥Read more about exhaustively checking enums.

查看 有关使用枚举的完整文档 了解更多信息。

¥Check out the the full docs on using enums to learn more.

何时使用 Flow 枚举

¥When to use Flow Enums

如果你之前定义了字面量的联合类型,则可以使用枚举来定义该类型。代替

¥If you previously defined a union type of literals, you can use an enum to define that type instead. Instead of

1type Status =2  | 'Active'3  | 'Paused'4  | 'Off';5
6const x: Status = 'Active';

或者

¥or

1const Status = Object.freeze({2  Active: 'Active',3  Paused: 'Paused',4  Off: 'Off',5});6type StatusType = $Keys<typeof Status>;7const x: StatusType = Status.Active;

你可以使用:

¥you can use:

1enum Status {2  Active,3  Paused,4  Off,5}6const x: Status = Status.Active;

请参阅 从旧版模式迁移 了解有关将旧版 JavaScript 枚举模式迁移到 Flow Enums 的更多信息。

¥See migrating from legacy patterns to learn more about migrating legacy JavaScript enum patterns to Flow Enums.

何时不使用 Flow 枚举

¥When to not use Flow Enums

枚举旨在涵盖许多用例并具有一定的优势。为了实现这一点,设计进行了各种权衡,在某些情况下,这些权衡可能不适合你。在这些情况下,你可以继续使用现有模式来满足你的用例。了解有关这些情况的更多信息

¥Enums are designed to cover many use cases and exhibit certain benefits. The design makes a variety of trade-offs to make this happen, and in certain situations, these trade-offs might not be right for you. In those cases, you can continue to use existing patterns to satisfy your use cases. Read more about those situations.