在带有括号和省略号的流中键入别名

Type alias in flow with brackets and ellipsis

type WeirdCustomType = {[int]: boolean, ...};

WeirdCustomType是什么样的结构?它只是一个 {int:boolean} 类型的数组吗? (即,键是一个整数,值是一个布尔值)?如果是这样,这里...的含义是什么?我在哪里可以阅读有关此特定类型别名用法的信息?

WeirdCustomType 是一个“Explicit inexact object type

其属性包括:

  • A) 具有带布尔值的整数键的属性
    • int 周围的括号表示我们指的是 属性 键,并且这些键是 int 类型(实际上应该是 number ,请参阅我的回答的最后一部分)
    • 这遵循对象及其键的解构语法,您可以在 MDN 文档上阅读有关“Computed object property names and destructuring”的更多信息
  • B) 具有其他键和值类型的附加属性

WeirdCustomType 的对象可能如下所示:

const inexactObject: WeirdObjectType = {
  1: ‘foo’,
  2: ‘bar’,
  baz: ‘abc’
}

WeirdCustomType 上的省略号明确表示(为了更加清楚)此类型允许具有额外属性的对象,而正常对象类型是预期的。

要禁用该行为,您可以使用 exact object type。如文档中所述,

Unlike regular object types, it is not valid to pass an object with “extra” properties to an exact object type.

// @flow
var foo: {| foo: string |} = { foo: "Hello", bar: "World!" }; // Error!

这里有一个 post on Medium 解释了动机。

Currently, {foo: number} is the type for any object which has a property foo with type number. {| foo: number |} is the type for an object which ONLY has a property foo with type number. We say the former syntax is an inexact object and the latter is an exact object.

In a few releases, Flow will begin to treat {foo: number} as an exact object. To indicate inexactness, you must add an ellipsis to the end of an object type: {foo: number, ...}. This new syntax forces the developers to opt in to inexactness.

关于 int 类型的注释

Flow 实际上没有原始类型 int。整数由 number 类型表示。我认为 [int] 应该是 [number].

https://flow.org/en/docs/types/primitives/