Incorporating Typescript into my project, I encountered a tedious issue while making an API call using axios
. The problem lies within handling nested data properly.
Despite my belief that I have correctly typed everything, I persistently face a Typescript error displaying
Unsafe return on an any typed value
and Unsafe member access .data on an any value
particularly in the line return response.data.data[0];
within my try/catch
block. Can someone shed light on why my types are not functioning as expected?
export interface Source {
curr: string;
uuid: string;
rate: string;
}
export interface Response {
data: {
data: Array<Source>;
};
}
async function getSource({ curr, uuid, rate }: Source): Promise<AxiosResponse<Response>> {
const requestConfig: AxiosRequestConfig = {
method: 'get',
url: '/url',
};
try {
const response = await axios(requestConfig);
return response.data.data[0];
} catch (err) {
throw new Error('error');
}
}