'How to get store API response of type array in typescript interface?
The api returns a response like this:
[
{
"id": "string",
"code": "string",
"name": "string",
}
]
So for example it will be
[
{
"id": "abc",
"code": "CODE1",
"name": "name1",
},
{
"id": "cdf",
"code": "CODE2",
"name": "name2",
}
]
How do I store it in a typescript interface? I initially tried it like this
export interface promotionCodes {
id: string,
code: string,
name: string,
}
export interface getPromotionsResponse {
data: promotionCodes[]
}
and I call it as:
return await this.get<getPromotionsResponse>(
`myurl`,
);
But I don't think this will work since I don't have anything named 'data' being returned in my call but I'm not sure how else to store it? Thanks for any help
Solution 1:[1]
You should use typescript generics for this purpose Here is the link to the docs: https://www.typescriptlang.org/docs/handbook/2/generics.html
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Malik Omer Javed |
