相当于 Flow 中的 TypeScript Record?

wca*_*lon 2 javascript typescript flowtype

TypeScript 提供了实用程序类型Record,我正在 Flow 中寻找其等效项。

我尝试过:{ [key: KeyType]: Value }但该定义具有不同的语义。

Jac*_*pie 6

等效项与 TypeScript 几乎相同:

// @flow

type Record<T, V> = {
  [T]: V
}
Run Code Online (Sandbox Code Playgroud)

从 TypeScript 文档中提取示例:

type ThreeStringProps = Record<'prop1' | 'prop2' | 'prop3', string>

const test: ThreeStringProps = {
  prop1: 'test',
  prop2: 'test',
  prop3: 'test',
}

// Fails because prop3 is not a string
const failingTest: ThreeStringProps = {
  prop1: 'test',
  prop2: 'test',
  prop3: 123,
}

// Fails because `prop4` isn't a valid property
const failingTest2: ThreeStringProps = {
  prop1: 'test',
  prop2: 'test',
  prop3: 'test',
  prop4: 'test',
}
Run Code Online (Sandbox Code Playgroud)

您可以在尝试流程中看到这一点的实际效果。