Skip to content

Commit 92b29d2

Browse files
Make the default type of response data never (#3002)
This requires TypeScript users to explicitly define the type of the data they are consuming. Before this, data was `any` by default. This means TypeScript consumers didn’t get type safety if they forgot to specify the type. Technically this is a breaking change for TypeScript users, as this will report errors if they forgot to specifiy the response type. The simplest workaround would be to explicitly set the response type to `any`, so it’s not breaking much. The `unknown` type is probably a slightly better fit, but this requires TypeScript ^3. `data` is still `any` in the very specific use case mentioned in microsoft/TypeScript#38969 Co-authored-by: Jay <jasonsaayman@gmail.com>
1 parent 4eeb3b1 commit 92b29d2

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

index.d.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export interface AxiosRequestConfig<T = any> {
8080
transitional?: TransitionalOptions
8181
}
8282

83-
export interface AxiosResponse<T = any> {
83+
export interface AxiosResponse<T = never> {
8484
data: T;
8585
status: number;
8686
statusText: string;
@@ -89,7 +89,7 @@ export interface AxiosResponse<T = any> {
8989
request?: any;
9090
}
9191

92-
export interface AxiosError<T = any> extends Error {
92+
export interface AxiosError<T = never> extends Error {
9393
config: AxiosRequestConfig;
9494
code?: string;
9595
request?: any;
@@ -98,7 +98,7 @@ export interface AxiosError<T = any> extends Error {
9898
toJSON: () => object;
9999
}
100100

101-
export interface AxiosPromise<T = any> extends Promise<AxiosResponse<T>> {
101+
export interface AxiosPromise<T = never> extends Promise<AxiosResponse<T>> {
102102
}
103103

104104
export interface CancelStatic {
@@ -142,14 +142,14 @@ export class Axios {
142142
response: AxiosInterceptorManager<AxiosResponse>;
143143
};
144144
getUri(config?: AxiosRequestConfig): string;
145-
request<T = any, R = AxiosResponse<T>> (config: AxiosRequestConfig<T>): Promise<R>;
146-
get<T = any, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig<T>): Promise<R>;
147-
delete<T = any, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig<T>): Promise<R>;
148-
head<T = any, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig<T>): Promise<R>;
149-
options<T = any, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig<T>): Promise<R>;
150-
post<T = any, R = AxiosResponse<T>>(url: string, data?: T, config?: AxiosRequestConfig<T>): Promise<R>;
151-
put<T = any, R = AxiosResponse<T>>(url: string, data?: T, config?: AxiosRequestConfig<T>): Promise<R>;
152-
patch<T = any, R = AxiosResponse<T>>(url: string, data?: T, config?: AxiosRequestConfig<T>): Promise<R>;
145+
request<T = never, R = AxiosResponse<T>> (config: AxiosRequestConfig<T>): Promise<R>;
146+
get<T = never, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig<T>): Promise<R>;
147+
delete<T = never, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig<T>): Promise<R>;
148+
head<T = never, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig<T>): Promise<R>;
149+
options<T = never, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig<T>): Promise<R>;
150+
post<T = never, R = AxiosResponse<T>>(url: string, data?: T, config?: AxiosRequestConfig<T>): Promise<R>;
151+
put<T = never, R = AxiosResponse<T>>(url: string, data?: T, config?: AxiosRequestConfig<T>): Promise<R>;
152+
patch<T = never, R = AxiosResponse<T>>(url: string, data?: T, config?: AxiosRequestConfig<T>): Promise<R>;
153153
}
154154

155155
export interface AxiosInstance extends Axios {

test/typescript/axios.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ axios.interceptors.response.use((response: AxiosResponse) => Promise.resolve(res
296296
// Adapters
297297

298298
const adapter: AxiosAdapter = (config: AxiosRequestConfig) => {
299-
const response: AxiosResponse = {
299+
const response: AxiosResponse<any> = {
300300
data: { foo: 'bar' },
301301
status: 200,
302302
statusText: 'OK',

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy