流类型 - 数组的对象

问题描述 投票:2回答:1

我有这样的对象:

{
  one: [
    {
      a: "some",
      b: "some",
    }
  ],
  two: [
    {
      a: "some",
      b: "some",
    }
  ],

  ...
}

此示例的正确流量类型是什么?

javascript flowtype
1个回答
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

Live demo


文档:

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.有一个专用的synta:{ [user_id: number]: string }

© www.soinside.com 2019 - 2024. All rights reserved.