I'm encountering some difficulty in typing a function that calls an external API and returns data based on the parameters sent. Here is what I have so far...
import axios from 'axios';
interface IGetContactArgs {
properties?: string[];
}
interface IResponse {
id: number,
properties: {
[index: string]: any;
}
}
const getContact = (id: number, params?: IGetContactArgs) => {
return axios.get<IResponse>(`URL/${id}`, { params });
}
For example, when invoking this function like so...
const data = await getContact(0, { properties: ['name', 'img' ] });
The expected result would be...
{
"id": 0,
"properties": {
"name": "John",
"img": "https://imageurl.com/image",
}
}
I'm wondering if there might be a more efficient way to type this function so that the output object matches the specific types of inputs provided?