Looking to extract data from an axios call in TypeScript
I have defined two interfaces:
interface Department{
code: string;
name: string;
country: string;
}
interface User {
name: string;
email: string;
departments: Department[];
}
The endpoint I am working with returns data structured like this:
[
{
"name": "Estonia",
"email": "email",
"phone": 12345,
"weight": "60kg",
"country": "US",
"departments": [
{
"code": 1,
"name": "depto 1",
"country": "US"
},
{
"code": 2,
"name": "depto 2",
"country": "FR"
}
]
[...]
}
]
Despite the large amount of data returned by the endpoint, I only want to retrieve specific attributes as specified in the interfaces. Is there a way to achieve this?
I attempted the following approach, but it retrieved all attributes:
axios.get<User[]>('http://localhost/users').then(({ data }) => {
console.log(typeof data, data);
});