user3152131 user3152131 - 4 months ago 15
AngularJS Question

Angular 2 trying to create interface for the expected API response

I am expecting the following example JSON response from API, and I am trying to make an interface for it.

{
types: [
{
id: 1,
contents: [
{
id: 1001,
perishable: 0
},
{
id: 1002,
perishable: 0
}
]
}
]
}


Here is how I am trying to define the interface, but I am not sure if I am doing this right. Basically, the response I get back is "types" which is an array of objects, and each object inside it has an array called "contents". Also, how to say something is an array of objects?

export interface Types {
id: string;
contents: Array<any>;
}


Is that right?

Answer

You can use any if you want, but you can be more specific:

interface Content {
    id: number;
    perishable: number;
}

interface Type {
    id: number;
    contents: Content[];
}

interface Response {
    types: Type[];
}