Skip to content
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

Uncaught TypeError: Cannot read properties of undefined (reading 'type') at Object.addCase (mapBuilders.ts:158:1) #2960

Closed
m-nathani opened this issue Nov 29, 2022 · 8 comments

Comments

@m-nathani
Copy link

m-nathani commented Nov 29, 2022

I am Defining a Pre-Typed createAsyncThunk

As of RTK 1.9, you can define a "pre-typed" version of createAsyncThunk that can have the types for state, dispatch, as shown in the usage-with-typescript#createasyncthunk

type RootState = ReturnType<typeof store.getState>;
type AppDispatch = AppStore['dispatch'];

export const createAppAsyncThunk = createAsyncThunk.withTypes<{
  state: RootState;
  dispatch: AppDispatch;
  rejectValue: { errorMessage: string };
}>();

And using the above to create a async thunk action as follows:

export const handleCommunication = createAppAsyncThunk<
  { template: Template; removeTemplateId: string | number } | null,
  { template: Template; messageLocale: string; communicationItemId: string | number }
>(
  'template/sendReservationMessage',
  async (
    { template, messageLocale, communicationItemId },
    { signal, rejectWithValue, getState }
  ) => {
    try {
      const state = getState();
      const something: unkown;

      // ... some business logics

      return something 
    } catch (err) {
      return rejectWithValue({
        errorMessage: (err as SerializedError).message || (err as string),
      });
    }
  }
);

However i am getting the error as :

mapBuilders.ts:158 Uncaught TypeError: Cannot read properties of undefined (reading 'type')
    at Object.addCase (mapBuilders.ts:158:1)
    at extraReducers (slice.ts:175:1)
    at executeReducerBuilderCallback (mapBuilders.ts:194:1)
    at buildReducer (createSlice.ts:341:1)
    at reducer (createSlice.ts:370:1)
    at redux.js:468:1
    at Array.forEach (<anonymous>)
    at assertReducerShape (redux.js:466:1)
    at combineReducers (redux.js:531:1)
    at configureStore (configureStore.ts:162:1)

P.S: note i dont have two thunks of the same typePrefix too...

What i am expecting is that all my actions should preload with AsyncThunkConfig defined in createAppAsyncThunk so that i dont have to types for { signal, rejectWithValue, getState } in all the thunks.

Looking forward to understand whats wrong in the approach or is a bug in redux-toolkit ?

Version:

    "@reduxjs/toolkit": "^1.9.0",
    "react": "^16.14.0",
    "react-redux": "^8.0.4",
    "typescript": "^4.8.4"
@phryneas
Copy link
Member

I'm sorry, we fixed this over in #2885 but did not have a new release yet.
You can use the fixed build by following the instructions over in https://ci.codesandbox.io/status/reduxjs/redux-toolkit/pr/2885/builds/313875

@m-nathani
Copy link
Author

I'm sorry, we fixed this over in #2885 but did not have a new release yet. You can use the fixed build by following the instructions over in https://ci.codesandbox.io/status/reduxjs/redux-toolkit/pr/2885/builds/313875

thanks @phryneas , do we know any expected date to release the fix?

Moreover, what i understood from the link is to install the package using this

npm i https://pkg.csb.dev/reduxjs/redux-toolkit/commit/9b1acb3d/@reduxjs/toolkit

@markerikson
Copy link
Collaborator

Yeah, I'm probably going to try to push out a 1.9.1 in the next day or so with the fixes we do have, and do a 1.9.2 later with any other fixes.

@m-nathani
Copy link
Author

Yeah, I'm probably going to try to push out a 1.9.1 in the next day or so with the fixes we do have, and do a 1.9.2 later with any other fixes.

Great, by the time we are using the package from this npm i https://pkg.csb.dev/reduxjs/redux-toolkit/commit/9b1acb3d/@reduxjs/toolkit

Thanks

@m-nathani
Copy link
Author

m-nathani commented Nov 30, 2022

@markerikson FYI: getting this error using 1.9.1

Uncaught Error: addCase cannot be called with two reducers for the same action type
    at Object.addCase (mapBuilders.ts:163:1)
    at extraReducers (slice.ts:197:1)
    at executeReducerBuilderCallback (mapBuilders.ts:194:1)
    at buildReducer (createSlice.ts:341:1)
    at reducer (createSlice.ts:370:1)
    at redux.js:468:1
    at Array.forEach (<anonymous>)
    at assertReducerShape (redux.js:466:1)
    at combineReducers (redux.js:531:1)
    at configureStore (configureStore.ts:162:1)

P.S: its working with this

npm i https://pkg.csb.dev/reduxjs/redux-toolkit/commit/9b1acb3d/@reduxjs/toolkit

@markerikson
Copy link
Collaborator

@m-nathani : a stack trace by itself really isn't helpful. What are you actually trying to do? Can you put together a CodeSandbox, a Github repo, or a Replay ( https://replay.io/record-bugs) that shows this happening?

@m-nathani
Copy link
Author

@m-nathani : a stack trace by itself really isn't helpful. What are you actually trying to do? Can you put together a CodeSandbox, a Github repo, or a Replay ( https://replay.io/record-bugs) that shows this happening?

i created a stackblitz and appears to be working fine...

And after debugging i found a typo on my side by using two rejected case of the same name.

It works with 1.9.1 👍🏼

@markerikson
Copy link
Collaborator

Cool! Sounds like I can go ahead and close this, then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants