Skip to content
Advertisement

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} >

Advertisement

Answer

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

interface SomeComponentProps {
  componentData: Array<{
    _id: string; 
    index: number;
    email?: string;
    nameList: Array<{
      id: number;
      name: string;
    }>;
  }>;
}
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement