-
-
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
Cannot resolve type circular dependency #1126
Comments
Assuming that slice and store creation will end up in different files in the end, the best approach to breaking the circle is to explicitly type the reducer. - export default slice.reducer
+ export default slice.reducer as Reducer<typeof initialState> |
Oh, that did worked out, thank you. |
Yeah, we really need to add this to the RTK and core TS usage guides. |
For people who are converting their existing slice from js to ts in addition to typing the reducer export also remember to change the extraReducers: {
[apiThunk.fetching]: (state, action) {
//Your logic
}
} to extraReducers: builders => {
builders.addCase(apiThunk.fetching, (state, action) {
//Your logic
}
)
} as mentioned here . |
Sorry I know it's closed but I really don't understand how to make it work My organization goes like this
In my store I am exporting the type of my slices export type RootState = ReturnType<typeof store.getState>; And in my slice it is exporting as suggested, but even so I don't get the types of my state in export const getSites = createAsyncThunk<SitesResponseProps, void, { state: RootState }>(
"sites/getSitesFromApi",
async (_, { getState }) => {
const { authentication } = getState();
}
);
> OR
export const getSites = createAsyncThunk<SitesResponseProps, void>(
"sites/getSitesFromApi",
async (_, { getState }) => {
const { authentication } = getState() as RootState;
}
);
// ...
export default siteSlice.reducer as Reducer<typeof initialState>;
I also tried to create a declare module "@reduxjs/toolkit" {
type AsyncThunkConfig = {
state?: unknown;
dispatch?: Dispatch;
extra?: unknown;
rejectValue?: unknown;
serializedErrorType?: unknown;
};
function createAsyncThunk<
Returned,
ThunkArg = void,
ThunkApiConfig extends AsyncThunkConfig = { state: RootState }
>(
typePrefix: string,
payloadCreator: AsyncThunkPayloadCreator<
Returned,
ThunkArg,
ThunkApiConfig
>,
options?: AsyncThunkOptions<ThunkArg, ThunkApiConfig>,
): AsyncThunk<Returned, ThunkArg, ThunkApiConfig>;
} |
@IuriKintschev this is a closed thread - pretty much everything that can be said has been said. If this does not solve it for you please create a new thread and provide a full reproduction. From just reading your code without a full application context I can't see any problems here. |
I'm quite new to
redux-toolkit
and have no idea how to solve this kind of circular dependency (code is heavily cut):This results in a
Type alias 'RootState' circularly references itself.ts (2456)
error. I need a full state in afetchSingleOrganization
function, not just this slice. I can see the problem, but don't know how to fix it. It would probably resolved fine, if there was an option to addextraReducers
afterwards, but I can't see one. What is a correct approach here?The text was updated successfully, but these errors were encountered: