Skip to content
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

Improved TypeScript typings for REST methods #6236

Open
SerhiyZheliznjak opened this issue Feb 15, 2024 · 3 comments
Open

Improved TypeScript typings for REST methods #6236

SerhiyZheliznjak opened this issue Feb 15, 2024 · 3 comments

Comments

@SerhiyZheliznjak
Copy link

Is your feature request related to a problem? Please describe.

Hi guys!

I noticed that typings on REST methods could be improved to make it simpler to use

let's take latest typings for post on v1.6.7 as an example

post<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;

so if I'd like have everything typped I have to do it this way:

type Params = {}
type Response = {}

axios.post<Response, AxiosResponse<Response>, Params>(....

don't you agree that being forced to Response, AxiosResponse<Response> is not cool?

Describe the solution you'd like

let's try to change typing definition of post to look like this:

 post<D = any, T = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<AxiosResponse<T, D>>;

so now if I'd like to have it typed I'd have to:

type Params = {}
type Response = {}

axios.post<Params, Response>(....

simple and elegant, right? :)

Describe alternatives you've considered

I've changed the order of D and T to match my way of thinking - first you need to input some data (D) and you will receive response (T)

here are all of my tweeks to your typings:

  request<T = any, D = any>(config: AxiosRequestConfig<D>): Promise<AxiosResponse<T, D>>;
  get<T = any, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<AxiosResponse<T, D>>;
  delete<T = any, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<AxiosResponse<T, D>>;
  head<T = any, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<AxiosResponse<T, D>>;
  options<D = any, T = any>(url: string, config?: AxiosRequestConfig<D>): Promise<AxiosResponse<T, D>>;
  post<D = any, T = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<AxiosResponse<T, D>>;
  put<D = any, T = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<AxiosResponse<T, D>>;
  patch<D = any, T = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<AxiosResponse<T, D>>;
  postForm<D = any, T = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<AxiosResponse<T, D>>;
  putForm<D = any, T = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<AxiosResponse<T, D>>;
  patchForm<D = any, T = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<AxiosResponse<T, D>>;

Additional context/Screenshots

No response

@jasonsaayman
Copy link
Member

sure seems nice enough, can you propose a solution in a pr?

@SerhiyZheliznjak
Copy link
Author

sure give me a moment :)

@SerhiyZheliznjak
Copy link
Author

@jasonsaayman here it is #6239

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants