简体   繁体   English

这个 object 的 typescript 接口会是什么样子?

[英]How would an typescript interface for this object look like?

I have an array of objects and I need to make an interface for it?我有一个对象数组,我需要为它创建一个接口? The array of objects:对象数组:

[
{
"_id": "62bd5fba34a8f1c90303055c",
"index": 0,
"email": "mcdonaldholden@xerex.com",
"nameList": [
  {
    "id": 0,
    "name": "Wendi Mooney"
  },
  {
    "id": 2,
    "name": "Holloway Whitehead"
  }
  ]
  },
  {
"_id": "62bd5fbac3e5a4fca5e85e81",
"index": 1,
"nameList": [
  {
    "id": 0,
    "name": "Janine Barrett"
  },
  {
    "id": 1,
    "name": "Odonnell Savage"
  },
  {
    "id": 2,
    "name": "Patty Owen"
  }
  ]
  },

Also I sent this object as props in another component, <SomeComponent componentData = {data} >我还发送了这个 object 作为另一个组件中的道具<SomeComponent componentData = {data} >

Break it down from the inside out...由内而外地分解...

interface Name {
  id: number;
  name: string;
}

interface Entry {
  _id: string;
  index: number;
  email?: string;
  nameList: Name[]; // or Array<Name>
}

interface SomeComponentProps {
  componentData: Entry[];
}

You could inline the whole thing but IMO that looks messy你可以内联整个东西,但 IMO 看起来很乱

interface SomeComponentProps {
  componentData: Array<{
    _id: string; 
    index: number;
    email?: string;
    nameList: Array<{
      id: number;
      name: string;
    }>;
  }>;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM