Issue With The Defenitelytyped Of Axios
I have installed axios both with npm and typings. Now, to use it in TypeScript, I need to pass to it a type (T). The problem is that this T is used for both the interface of the re
Solution 1:
It seems the current implementation does not fully use generics. But you can extend it.
Install axios types with
npm i@types/axios
Then create a file typings\axios.d.ts
with
declare namespace Axios {
interface AxiosInstance {
<TRequest, TResponse>(config: AxiosXHRConfig<TRequest>): IPromise<AxiosXHR<TResponse>>;
post<TRequest, TResponse>(url: string, data?: TRequest, config?: AxiosXHRConfigBase<TResponse>): IPromise<AxiosXHR<TResponse>>;
}
}
This will allow you to use:
import * as axios from'axios';
interfaceDataToSend {
name: string;
}
interfaceDataToReceive {
status: number;
}
axios<DataToReceive>({ url: '' }).then(r => r.data.status);
axios<DataToSend, DataToReceive>({
url: '',
method: 'POST',
data: { name: '' },
}).then(r => r.data.status);
axios.post<DataToSend, DataToReceive>('', { name: 'test' })
.then(r => r.data.status);
Post a Comment for "Issue With The Defenitelytyped Of Axios"