0

I was reading this article about how to use Axios with TypeScript and I had a question about the methods declaration, for example:

  get<T = any, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig): Promise<R> {
    return this.http.get<T, R>(url, config);
  }

  delete<T = any, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig): Promise<R> {
    return this.http.delete<T, R>(url, config);
  }

Now, what I miss to understand is the this syntax:

<T = any, R = AxiosResponse<T>>

Why is the T equal to any, isn't it the concept of Generics that T can be anything? Why do I need to specify it? Is it for readability? Also why is R equals AxiosResponse<T>? Couldn't I just say that the return type of the function is Promise<AxiosResponse<T>>, is it also the sake of writing less code in the return type?

1
  • 1
    Those are defaults. It's like if you write function(x: number = 1) { ... }, it doesn't mean x has to be 1, it means that x is 1 unless you say otherwise. Commented Jan 29, 2022 at 10:54

1 Answer 1

1

Those are defaults that are used when the type isn't specified and can't be inferred. (Kind of like function parameter defaults, function foo(a = 42) says a is 42 if [loosely speaking] it's not provided.)

...isn't it the concept of Generics that T can be anything?

Not necessarily, generic type parameters can be constrained, but yes in this case.

Why do I need to specify it?

You don't need to (but you can), there's a default if you don't and it can't be inferred from the call site. If the = any weren't there, then you would have to specify it.

Also why is R equals AxiosResponse<T>? Couldn't I just say that the return type of the function is Promise<AxiosResponse<T>>...

It's simpler to use (and probably better for inference) if you specify the type of what you're expecting in the response, without the Promise wrapper on it.

const thingy = await axios.get<ThingyType>("...");
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer. I want to clarify something, in the article he uses the return type as such func <T= any, R = AxiosPromise<T>>(...args):Promise<R> {...} What was I wondering about the R is if it could've also be written as such: func <T= any>(...args):Promise<AxiosPromise<T>> {...}?
@Keselme - That would mean something different. Again, AxiosPromise<T> is just the default for R if R isn't specified. But the way the article has it, the caller can specify R to be something other than AxiosPromise<T>. (I find it odd that the article seems to be wrapping a promise [AxiosPromise] in a promise [Promise], but...) FWIW, beware that the bar for posting an article on the web is really low and the quality varies. That article may be fantastic (I haven't read it and don't know the author), but there are definitely very poor ones out there...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.