我有这样的对象:
{
one: [
{
a: "some",
b: "some",
}
],
two: [
{
a: "some",
b: "some",
}
],
...
}
此示例的正确流类型是什么?
答案 0 :(得分:2)
你去了
/* @flow */
type MyType = {
[key: string]: Array<{ a: string, b: string }>,
};
function myFunc(obj: MyType) {
return obj;
}
const myObj = {
one: [
{
a: 'some',
b: 'some',
}
],
two: [
{
a: 'some',
b: 'some',
}
],
};
const doSomething = myFunc(myObj); // No errors
在Objects as maps doc中,您会发现For objects like these, Flow provides a special kind of property, called an “indexer property.” An indexer property allows reads and writes using any key that matches the indexer key type.
使用专用的语法:{ [user_id: number]: string }