gpt4 book ai didi

javascript - 相当于 Flow 中的 TypeScript Record?

转载 作者:行者123 更新时间:2023-12-03 01:45:57 24 4
gpt4 key购买 nike

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

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

最佳答案

等价物几乎与 TypeScript 相同:

// @flow

type Record<T, V> = {
[T]: V
}

从 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',
}

您可以在Try Flow查看此操作的实际效果。 .

关于javascript - 相当于 Flow 中的 TypeScript Record?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50639585/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com