From a5eb6895de26403ed704a54ec10da1b92d38f3b0 Mon Sep 17 00:00:00 2001 From: Remco Haszing Date: Sun, 7 Jun 2020 14:46:25 +0200 Subject: [PATCH] Make the default type of response data never MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 https://github.com/microsoft/TypeScript/issues/38969 --- index.d.ts | 22 +++++++++++----------- test/typescript/axios.ts | 2 +- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/index.d.ts b/index.d.ts index 74b2acd2cc..088e48cd7c 100644 --- a/index.d.ts +++ b/index.d.ts @@ -73,7 +73,7 @@ export interface AxiosRequestConfig { decompress?: boolean; } -export interface AxiosResponse { +export interface AxiosResponse { data: T; status: number; statusText: string; @@ -82,7 +82,7 @@ export interface AxiosResponse { request?: any; } -export interface AxiosError extends Error { +export interface AxiosError extends Error { config: AxiosRequestConfig; code?: string; request?: any; @@ -91,7 +91,7 @@ export interface AxiosError extends Error { toJSON: () => object; } -export interface AxiosPromise extends Promise> { +export interface AxiosPromise extends Promise> { } export interface CancelStatic { @@ -136,14 +136,14 @@ export interface AxiosInstance { response: AxiosInterceptorManager; }; getUri(config?: AxiosRequestConfig): string; - request> (config: AxiosRequestConfig): Promise; - get>(url: string, config?: AxiosRequestConfig): Promise; - delete>(url: string, config?: AxiosRequestConfig): Promise; - head>(url: string, config?: AxiosRequestConfig): Promise; - options>(url: string, config?: AxiosRequestConfig): Promise; - post>(url: string, data?: any, config?: AxiosRequestConfig): Promise; - put>(url: string, data?: any, config?: AxiosRequestConfig): Promise; - patch>(url: string, data?: any, config?: AxiosRequestConfig): Promise; + request> (config: AxiosRequestConfig): Promise; + get>(url: string, config?: AxiosRequestConfig): Promise; + delete>(url: string, config?: AxiosRequestConfig): Promise; + head>(url: string, config?: AxiosRequestConfig): Promise; + options>(url: string, config?: AxiosRequestConfig): Promise; + post>(url: string, data?: any, config?: AxiosRequestConfig): Promise; + put>(url: string, data?: any, config?: AxiosRequestConfig): Promise; + patch>(url: string, data?: any, config?: AxiosRequestConfig): Promise; } export interface AxiosStatic extends AxiosInstance { diff --git a/test/typescript/axios.ts b/test/typescript/axios.ts index c584dfc578..1984a23ba9 100644 --- a/test/typescript/axios.ts +++ b/test/typescript/axios.ts @@ -284,7 +284,7 @@ axios.interceptors.response.use((response: AxiosResponse) => Promise.resolve(res // Adapters const adapter: AxiosAdapter = (config: AxiosRequestConfig) => { - const response: AxiosResponse = { + const response: AxiosResponse = { data: { foo: 'bar' }, status: 200, statusText: 'OK',