流类型对象中的动态属性名称 [英] Dynamic property names in flow typed object
本文介绍了流类型对象中的动态属性名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在慢慢地将flowType工作到我的代码中,但我正在努力解决一个概念。
I'm slowly and surely working flowType into my code, but I'm struggling with one concept.
如何指定未知的类型,动态命名对象的属性?
How do I specify the type of unknown, dynamically named properties of an object?
例如,我的用户对象可能包含一个包含具有唯一键的组织的对象。
For example my user object might have an object containing organisations with unique keys.
我该如何定义?
export type User = ?{
currentOrg: string,
displayName?: string,
email: string,
emailVerified: boolean,
newAccount: boolean,
organisations?: {
UNKNOWNKEY?: string {
orgData1: string,
orgData2: string,
}
},
uid: string,
photoUrl?: string,
};
真的很感激任何帮助。谢谢!
Would really appreciate any help on this. Thanks!
推荐答案
Flow有特定语法:
{ [key: K]: V }
其中 K
是键的类型, V
是值的类型。
where K
is they type of the keys and V
is the type of the values.
你的全部示例如下所示:
Your full example would look like:
export type User = {
currentOrg: string,
displayName?: string,
email: string,
emailVerified: boolean,
newAccount: boolean,
organisations?: { [key: string]: string },
uid: string,
photoUrl?: string,
};
这篇关于流类型对象中的动态属性名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文