-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
RTK Query: circular type reference #2191
Comments
I'm not quite sure what the question is here. Are you seeing an actual error right now, or looking ahead and wondering if this might cause a problem? What have you tried so far? |
This is an actual error. |
Can you put together a CodeSandbox that shows this error happening? |
see #1685 (comment) |
That will probably go away once you type your mutation explicitly so TS knows that there is no need to take the RootState into account for infering the result type. saveRecords: mutation<Result, Argument>({ |
That doesn't help. The issue arises whenever the result of a |
Yeah, seems like it needs one or two more explicit type annotation to break the circle. It's a bit weird since it should really not need that, but sometimes TS is just TS unfortunately. const api = createApi({
reducerPath: "api",
baseQuery: fetchBaseQuery({ baseUrl: "/" }),
endpoints: ({ query, mutation }) => ({
saveRecords: mutation<SaveResult, undefined>({
queryFn: async (
args,
queryApi,
extra,
baseQuery
): Promise<
QueryReturnValue<SaveResult, FetchBaseQueryError, FetchBaseQueryMeta>
> => {
const getState = queryApi.getState as () => AppState;
const result = await baseQuery("/save/" + getState().foo.bar);
return result as QueryReturnValue<
SaveResult,
FetchBaseQueryError,
FetchBaseQueryMeta
>;
}
})
})
}); An alternative would be to cast not to AppState, but to something like |
Here's a workaround that somehow worked for me. I created a interface AppQueryApi extends BaseQueryApi {
getState(): AppState; // for whatever reason this doesn't trigger the circular reference error
}
type FetchBaseQuery = <T>(
arg: string | FetchArgs
) => MaybePromise<QueryReturnValue<T, FetchBaseQueryError, FetchBaseQueryMeta>>;
type QueryFn<out Result, in Arg> = (
arg: Arg,
queryApi: AppQueryApi,
extra: any,
baseQuery: FetchBaseQuery
) => MaybePromise<QueryReturnValue<Result, FetchBaseQueryError, FetchBaseQueryMeta>>;
const saveRecordsQueryFn: QueryFn<SaveResult, undefined> =
async (args, { getState }, extra, baseQuery) => {
const result = await baseQuery<SaveResult>("/save/" + getState().foo.bar);
return result;
};
const api = createApi({
baseQuery: fetchBaseQuery({ baseUrl: "/" }),
endpoints: ({ query, mutation }) => ({
saveRecords: mutation({ queryFn: saveRecordsQueryFn })
})
}); As a nice bonus, this approach allows me to extract complex |
So here's an update on the approach from my previous message after trying to use it for a while. TL;RD: kind of works, but not really usable While Currently, in my IDE (VS Code), when the file with the |
I'm going to close this because I don't think there's anything immediately actionable for us to fix here. |
I saw #1126 and #1518, they are both about async thunks, and probably can be solved with workaround about how reducer is exported (didn't try it myself).
I need AppState in RTQ endpoint definition, like this:
I don't even have initial state to try this approach.
What can I do in this case, aside of casting state into
any
?Obviously, in this simple example I can rethink my api and just pass user id as explicit parameter. But in general case, is it possible to solve?
The text was updated successfully, but these errors were encountered: