Skip to content

Commit

Permalink
Make the default type of response data never (#3002)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
remcohaszing and jasonsaayman committed Sep 5, 2021
1 parent 4eeb3b1 commit 92b29d2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
22 changes: 11 additions & 11 deletions index.d.ts
Expand Up @@ -80,7 +80,7 @@ export interface AxiosRequestConfig<T = any> {
transitional?: TransitionalOptions
}

export interface AxiosResponse<T = any> {
export interface AxiosResponse<T = never> {
data: T;
status: number;
statusText: string;
Expand All @@ -89,7 +89,7 @@ export interface AxiosResponse<T = any> {
request?: any;
}

export interface AxiosError<T = any> extends Error {
export interface AxiosError<T = never> extends Error {
config: AxiosRequestConfig;
code?: string;
request?: any;
Expand All @@ -98,7 +98,7 @@ export interface AxiosError<T = any> extends Error {
toJSON: () => object;
}

export interface AxiosPromise<T = any> extends Promise<AxiosResponse<T>> {
export interface AxiosPromise<T = never> extends Promise<AxiosResponse<T>> {
}

export interface CancelStatic {
Expand Down Expand Up @@ -142,14 +142,14 @@ export class Axios {
response: AxiosInterceptorManager<AxiosResponse>;
};
getUri(config?: AxiosRequestConfig): string;
request<T = any, R = AxiosResponse<T>> (config: AxiosRequestConfig<T>): Promise<R>;
get<T = any, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig<T>): Promise<R>;
delete<T = any, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig<T>): Promise<R>;
head<T = any, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig<T>): Promise<R>;
options<T = any, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig<T>): Promise<R>;
post<T = any, R = AxiosResponse<T>>(url: string, data?: T, config?: AxiosRequestConfig<T>): Promise<R>;
put<T = any, R = AxiosResponse<T>>(url: string, data?: T, config?: AxiosRequestConfig<T>): Promise<R>;
patch<T = any, R = AxiosResponse<T>>(url: string, data?: T, config?: AxiosRequestConfig<T>): Promise<R>;
request<T = never, R = AxiosResponse<T>> (config: AxiosRequestConfig<T>): Promise<R>;
get<T = never, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig<T>): Promise<R>;
delete<T = never, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig<T>): Promise<R>;
head<T = never, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig<T>): Promise<R>;
options<T = never, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig<T>): Promise<R>;
post<T = never, R = AxiosResponse<T>>(url: string, data?: T, config?: AxiosRequestConfig<T>): Promise<R>;
put<T = never, R = AxiosResponse<T>>(url: string, data?: T, config?: AxiosRequestConfig<T>): Promise<R>;
patch<T = never, R = AxiosResponse<T>>(url: string, data?: T, config?: AxiosRequestConfig<T>): Promise<R>;
}

export interface AxiosInstance extends Axios {
Expand Down
2 changes: 1 addition & 1 deletion test/typescript/axios.ts
Expand Up @@ -296,7 +296,7 @@ axios.interceptors.response.use((response: AxiosResponse) => Promise.resolve(res
// Adapters

const adapter: AxiosAdapter = (config: AxiosRequestConfig) => {
const response: AxiosResponse = {
const response: AxiosResponse<any> = {
data: { foo: 'bar' },
status: 200,
statusText: 'OK',
Expand Down

0 comments on commit 92b29d2

Please sign in to comment.