New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Typescript types do not make sense #4176
Comments
After upgrading from 0.21.1 to 0.22.0, my existing codebase is showing Typescript errors now. The return type from This code used to work without problems on 0.21.1:
Now with 0.22.0 I get this:
Something got messed up with the typings for the response... |
I think the typings for |
this is actually my current issue. and I thought maybe I'm mistaken. and for the past two hours, I was searching the web for a possible fix. |
downgrade to 0.21.1, works again. |
post<T = never, R = AxiosResponse<T>>(url: string, data?: T, config?: AxiosRequestConfig<T>): Promise<R>; that means post() returns a promise which has a type of maybe we can temporary solve this problem by using
|
You certainly can solve this issue by adding const { data }: any = await instance.get("/users/me"); Glad to see I'm not the only one with it! It'd be nice to see what the axios team think to this issue. I've also had issues adding interceptors - also type issues: (config: any) => {
const token = useStore.getState().authToken;
if (token) {
config.headers.jwtAuthToken = token;
} else {
if (instance && instance.defaults.headers) {
delete instance.defaults.headers.jwtAuthToken;
}
}
return config;
}, without |
If I'm not wrong, the types definition seems correct on master Line 182 in 94a9344
Hopefully, we need just to wait for the next release |
Yeah the next release should mend this, will probably release in the next hour or so |
So, maybe I faced a similar problem: public async getContestTypeByName(name: string): Promise<ContestType> {
return new Promise<ContestType>((resolve, reject) => {
this.api
.get(`${Endpoints.ContestType}/${name}`)
.then((response: AxiosResponse<ContestType>) => resolve(response.data)) // ts error in this line
.catch((error: AxiosError<string>) => reject(error));
});
} TypeScript Error log:
Fixed by typing the axios.get method itself: public async getContestTypeByName(name: string): Promise<ContestType> {
return new Promise<ContestType>((resolve, reject) => {
this.api
.get<ContestType>(`${Endpoints.ContestType}/${name}`) // <- here
.then((response) => resolve(response.data))
.catch((error: AxiosError<string>) => reject(error));
});
} Don't know if it's the right way, but works fine. |
Ah, this looks like a legit bug with how the export interface AxiosInterceptorManager<V> {
use<T = V>(onFulfilled?: (value: V) => T | Promise<T>, onRejected?: (error: any) => any): number;
eject(id: number): void;
}
export class Axios {
constructor(config?: AxiosRequestConfig);
defaults: AxiosDefaults;
interceptors: {
request: AxiosInterceptorManager<AxiosRequestConfig>;
response: AxiosInterceptorManager<AxiosResponse>;
};
// ...rest of methods
} There's no way to inject the type of response you want to intercept, it defaults to This might be correct typing, though, since the response interceptors see all types of data coming through. Regardless, you are correct, there's no way to type that signature, and forcing everyone to write type guards to narrow down from If I understand the axios docs correctly this signature for export interface AxiosInterceptorManager<V extends AxiosResponse | AxiosRequestConfig> {
use<T, D = any>(
onFulfilled?: (value: V extends AxiosResponse ? AxiosResponse<T, D> : AxiosRequestConfig<T>) => typeof value | Promise<typeof value>,
onRejected?: (error: any) => any
): number;
eject(id: number): void;
} That being said one should still call the interceptor like so: type ResponseTypeA = {
token?: string;
};
type ResponseTypeB = {
token?: number;
};
type ResponseTypeC = {
token?: Array<number>;
};
this.client.interceptors.response.use<
ResponseTypeA | ResponseTypeB | ResponseTypeC
>(async (response) => {
// Some code
return response;
}); Because, literally, all types of responses pass through your interceptor. |
I have another problem with the interceptor typings: they do not allow for a 3rd 'options' parameter, despite what the documentation says : https://github.com/axios/axios#interceptors. |
Yesterday i was thinking i'm so stupid i can't figure it out. Thank you for recovering my self-esteem 😎 |
This should be mended in the latest version |
Is there a new release? I still see 0.23.0 |
AxiosResponse reverted to any in v0.24.0 |
Thanks! I pulled in v0.24.0 and my code is back to working as before in v0.21.4. |
Hi 👋 Please could you retry with the latest pre-release version and open a new issue should this error still be relevant? Thanks |
Describe the bug
I updated from axios
0.21.1
to0.22.0
but typescript started giving me strange errorsI then noticed that the types declarations changed and they do not make much sense.
I can see that the
post
signature is:I don't really get why the response data object should be, by default, equal to the object passed as body, there is really no sense for that as a default behaviour. It should be AxiosResponse instead.
For the
axios
function, the returned object is even anever
, inaxios(config: AxiosRequestConfig<any>): AxiosPromise<never>
.Of course I can just put the types specifications, but it makes the code longer and ugly when there would be many times no reason for that.
To Reproduce
To reproduce it, just try to make a post request
Expected behavior
There should not be types assumptions such as the one made
Environment
Additional context/Screenshots
Add any other context about the problem here. If applicable, add screenshots to help explain.
The text was updated successfully, but these errors were encountered: