Skip to content

Cannot resolve type circular dependency #1126

Closed
@atiupin

Description

@atiupin

I'm quite new to redux-toolkit and have no idea how to solve this kind of circular dependency (code is heavily cut):

import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";

interface Organization {
  name: string;
}

const fetchSingleOrganization = createAsyncThunk<Organization, string, { state: RootState }>(
  "organizations/fetch-single",
  async (id, { getState }) => {
    // fetch code
  },
);

const organizationsSlice = createSlice({
  name: "organizations",
  initialState: null,
  reducers: {},
  extraReducers: (builder) =>
    builder.addCase(fetchSingleOrganization.fulfilled, () => {
      // upsert code
    }),
});

const store = configureStore({
  reducer: {
    organizations: organizationsSlice.reducer,
  },
});

type RootState = ReturnType<typeof store.getState>;

This results in a Type alias 'RootState' circularly references itself.ts (2456) error. I need a full state in a fetchSingleOrganization 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 add extraReducers afterwards, but I can't see one. What is a correct approach here?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions