Gochi Siyan
Gochi Siyan

Reputation: 29

How to combine Typescript generic arguments?

const response = await axios.get<any, AxiosResponse<any, any>, any>('login');

to

const response = await axios.get<CombinedGenerics>('login');

This is needed to reduce the boilerplate.

There does not seem to be a way to destructure the arguments of the type either.

I have tried to scour on instantiation expression (https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-7.html), but it is not what I am looking for.

Upvotes: 1

Views: 32

Answers (1)

ayman omara
ayman omara

Reputation: 344

you may use something like

type CombinedGenerics<T = any, R = AxiosResponse<T>, D = any> = {
  responseType: T;
  axiosResponseType: R;
  dataType: D;
};

Upvotes: 1

Related Questions