I am currently working on building a TypeScript wrapper for RTKQ queries that can be used generically. I have made progress on most of my goals, but I am encountering an issue with determining the return type for a generic or specific query. I have shared some of the key lines of code from my test below, but if you want to see the complete code, you can check out this code sandbox.
type EndpointKey = keyof typeof api.endpoints;
type Endpoint<EndpointName extends EndpointKey> = typeof injectedAPI.endpoints[EndpointName];
type ReturnQuery<E extends EndpointKey> = Endpoint<E>["useQuery"];
// The return type appears to be Record<string, any> & UseQuerySubscriptionResult<>, but why is the UseQueryStateResult<> missing?
type PuppiesQueryReturnType = ReturnType<ReturnQuery<"getPuppies">>;
const queryReturn: PuppiesQueryReturnType = {} as PuppiesQueryReturnType;
const { data, foo, refetch, isLoading, error } = queryReturn; // All values have type any except for refetch
I'm unsure whether this discrepancy in types is a bug in the RTKQ typings or if I may have missed a step or made an error in my implementation.
Please note that not all of the code provided is originally mine; I have gathered inspiration from various online sources.