javascript - any type of I can designate in generic types in axios.get method with TypeScript - Stack Overflow

axios.get('api')When I code with TypeScript like above, I should better designate types as I

axios.get('/api')

When I code with TypeScript like above, I should better designate types as I can reference the type definition of axios as below.

(method) AxiosInstance.get<any, AxiosResponse<any>>(url: string, config?: AxiosRequestConfig | undefined): Promise<AxiosResponse<any>>
                           ^^^ <- ???

I can't understand the any type of the 1st one of generic types of get method AxiosInstance.get<any,. What for shall this any be used?

axios.get('/api')

When I code with TypeScript like above, I should better designate types as I can reference the type definition of axios as below.

(method) AxiosInstance.get<any, AxiosResponse<any>>(url: string, config?: AxiosRequestConfig | undefined): Promise<AxiosResponse<any>>
                           ^^^ <- ???

I can't understand the any type of the 1st one of generic types of get method AxiosInstance.get<any,. What for shall this any be used?

Share Improve this question asked Aug 4, 2019 at 8:41 diveintohackingdiveintohacking 5,2137 gold badges32 silver badges43 bronze badges 1
  • 1 Instead of any you should put there the type of the Model/Data that this request returns; like axios.get<IUser>('/api/user/12345') And leave the second type empty. Maybe it would have been better to not put in this second Type there at all and instead describe the method as get<T = any>(url: string, config?: AxiosRequestConfig): Promise<AxiosResponse<T>>; – Thomas Commented Aug 4, 2019 at 9:07
Add a ment  | 

2 Answers 2

Reset to default 5

Take a look at the axios type definitions.

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

The first type argument is the type that is returned by the api. This defaults to any.

The second is the type of response. This defaults to a response that carries the type if the first argument.

The first argument of Axios type generics refers only to the data property of the response object (which is the result of a request call), this may be a little be redundant if you also use the second argument, because the second one refers to the entire response object, including not only data, but config, headers, status and statusText. There is also a third parameter that will be reflected in the request parameters, the use cases are demonstrated below:

import { Axios, AxiosResponse } from 'axios';

type MyStatus = 200 | 404
type MyResponseData = { test: string, total: number }
type MyRequestData = { page: number }

interface CustomResponse extends AxiosResponse {
  status: MyStatus;
  // you can also override response data here
}

(new Axios('/')).get<
  MyResponseData,
  CustomResponse,
  MyRequestData,
>('/test',
  // The typing here follows MyRequestData
  { data: {page: 1} }
// The typings of res follows MyResponseData
// and also, res.data has CustomResponse types
).then(res => {
  // typeof res.data = MyResponseData
  // typeof res.status = MyStatus
})

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744310558a4567925.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信