Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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;
    }>;
  }>;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading