Interface for object typescript/ionic 3

102
June 15, 2018, at 10:30 PM

I have a json object returned from an api and want to create an interface with fields that the object contains. I am using ionic 3 framework. I want help in how to create this interface.( i am confused: should i create another interface for data? and how to include it in the main interface if yes?) the object structure is as follows:

{
"status": "success",
"data": [
      {
          "id": 113,
          "subject": "hello there",
          "body": "i am hisham",
          "sender": {
              "id": 51,
              "country": {
                  "id": 9,
                  "name_en": "Syria",
              }
          }
      },
      {
          "id": 114,
          "subject": "hello there",
          "body": "i am lkfdj",
          "sender": {
              "id": 54,
              "country": {
                  "id": 9,
                  "name_en": "Syria",
              }
          }
      }
  ]
}
Answer 1

If you're defining interfaces, you should define one for each object in your response. You don't have to, but to get proper type completion, you should.

interface Response {
  status: string;
  data: Data[];
}
interface Data {
  id: number;
  subject: string;
  body: string;
  sender: Sender;
}
interface Sender {
  id: number;
  country: Country;
}
interface Country {
  id: number;
  name_en: string;
}
READ ALSO
history.replaceState() doesn't trigger a popstate event in Firefox?

history.replaceState() doesn't trigger a popstate event in Firefox?

I have a SPA / web app which accesses a REST APIThe first request to the server returns a shell, and all subsequent requests are AJAX calls, which return JSON data, which is then rendered on the client

107
Separate strings in an array in a certain order

Separate strings in an array in a certain order

I have a list that I took from a converted CHANGELOGmd file, and it looks like this:

82
Selenium Webdriver - Setting Cookie With Chrome Extension

Selenium Webdriver - Setting Cookie With Chrome Extension

I am trying to use a chrome extension to set a cookie when I get a Selenium Webdriver instance to open a pageI have tried following a variety of ways suggested in different stackoverflow posts but none of them ever show when I open the page inspector

221