Skip to content

Commit b0dccc6

Browse files
author
Daniel Fischer
committed
Updated TypeScript typings with generic type parameters for request methods & AxiosResponse
1 parent 07a7b7c commit b0dccc6

2 files changed

Lines changed: 48 additions & 9 deletions

File tree

index.d.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export interface AxiosTransformer {
33
}
44

55
export interface AxiosAdapter {
6-
(config: AxiosRequestConfig): AxiosPromise;
6+
(config: AxiosRequestConfig): AxiosPromise<any>;
77
}
88

99
export interface AxiosBasicCredentials {
@@ -44,8 +44,8 @@ export interface AxiosRequestConfig {
4444
cancelToken?: CancelToken;
4545
}
4646

47-
export interface AxiosResponse {
48-
data: any;
47+
export interface AxiosResponse<T = any> {
48+
data: T;
4949
status: number;
5050
statusText: string;
5151
headers: any;
@@ -59,7 +59,7 @@ export interface AxiosError extends Error {
5959
response?: AxiosResponse;
6060
}
6161

62-
export interface AxiosPromise extends Promise<AxiosResponse> {
62+
export interface AxiosPromise<T = any> extends Promise<AxiosResponse<T>> {
6363
}
6464

6565
export interface CancelStatic {
@@ -101,13 +101,13 @@ export interface AxiosInstance {
101101
request: AxiosInterceptorManager<AxiosRequestConfig>;
102102
response: AxiosInterceptorManager<AxiosResponse>;
103103
};
104-
request(config: AxiosRequestConfig): AxiosPromise;
105-
get(url: string, config?: AxiosRequestConfig): AxiosPromise;
104+
request<T = any>(config: AxiosRequestConfig): AxiosPromise<T>;
105+
get<T = any>(url: string, config?: AxiosRequestConfig): AxiosPromise<T>;
106106
delete(url: string, config?: AxiosRequestConfig): AxiosPromise;
107107
head(url: string, config?: AxiosRequestConfig): AxiosPromise;
108-
post(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise;
109-
put(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise;
110-
patch(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise;
108+
post<T = any>(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise<T>;
109+
put<T = any>(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise<T>;
110+
patch<T = any>(url: string, data?: any, config?: AxiosRequestConfig): AxiosPromise<T>;
111111
}
112112

113113
export interface AxiosStatic extends AxiosInstance {

test/typescript/axios.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,45 @@ axios.patch('/user', { foo: 'bar' })
9797
.then(handleResponse)
9898
.catch(handleError);
9999

100+
// Typed methods
101+
interface User {
102+
id: number;
103+
name: string;
104+
}
105+
106+
const handleUserResponse = (response: AxiosResponse<User>) => {
107+
console.log(response.data.id);
108+
console.log(response.data.name);
109+
console.log(response.status);
110+
console.log(response.statusText);
111+
console.log(response.headers);
112+
console.log(response.config);
113+
};
114+
115+
axios.get<User>('/user?id=12345')
116+
.then(handleUserResponse)
117+
.catch(handleError);
118+
119+
axios.get<User>('/user', { params: { id: 12345 } })
120+
.then(handleUserResponse)
121+
.catch(handleError);
122+
123+
axios.post<User>('/user', { foo: 'bar' })
124+
.then(handleUserResponse)
125+
.catch(handleError);
126+
127+
axios.post<User>('/user', { foo: 'bar' }, { headers: { 'X-FOO': 'bar' } })
128+
.then(handleUserResponse)
129+
.catch(handleError);
130+
131+
axios.put<User>('/user', { foo: 'bar' })
132+
.then(handleUserResponse)
133+
.catch(handleError);
134+
135+
axios.patch<User>('/user', { foo: 'bar' })
136+
.then(handleUserResponse)
137+
.catch(handleError);
138+
100139
// Instances
101140

102141
const instance1: AxiosInstance = axios.create();

0 commit comments

Comments
 (0)