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

Type error of slice file #516

Open
alexlam184 opened this issue Dec 29, 2022 · 2 comments
Open

Type error of slice file #516

alexlam184 opened this issue Dec 29, 2022 · 2 comments
Labels

Comments

@alexlam184
Copy link

import { createSlice } from "@reduxjs/toolkit";
import { AppState } from "./store";
import { HYDRATE } from "next-redux-wrapper";

// Type for our state
export interface AuthState {
  authState: boolean;
}

// Initial state
const initialState: AuthState = {
  authState: false,
};

// Actual Slice
export const authSlice = createSlice({
  name: "auth",
  initialState,
  reducers: {
    // Action to set the authentication status
    setAuthState(state, action) {
      state.authState = action.payload;
    },

    // Special reducer for hydrating the state. Special case for next-redux-wrapper
    extraReducers: {
      [HYDRATE]: (state, action) => {
        return {
          ...state,
          ...action.payload.auth,
        };
      },
    },
  },
});

export const { setAuthState } = authSlice.actions;

export const selectAuthState = (state: AppState) => state.auth.authState;

export default authSlice.reducer;

in extraReducers , type error occur. the error message is below
"""
Type '{ NEXT_REDUX_WRAPPER_HYDRATE: (state: any, action: any) => any; }' is not assignable to type 'CaseReducer<AuthState, { payload: any; type: string; }> | CaseReducerWithPrepare<AuthState, PayloadAction<any, string, any, any>>'.
Object literal may only specify known properties, and '[HYDRATE]' does not exist in type 'CaseReducer<AuthState, { payload: any; type: string; }> | CaseReducerWithPrepare<AuthState, PayloadAction<any, string, any, any>>'.
"""
image

Please help

this is nextjs Typescript project

@alexlam184 alexlam184 added the bug label Dec 29, 2022
@danyloburavlov
Copy link

@alexlam184
Had the same issue, try this

extraReducers: { [HYDRATE]: (state: AuthState, action: PayloadAction<{ auth: AuthState }>) => ({ ...state, ...action.payload.auth, }), },

@tuot
Copy link

tuot commented Feb 15, 2023

@alexlam184
reducers and extraReducers should be at the same level.
Like this:

{
    reducers: {}
    extraReducers: {}
}

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

No branches or pull requests

3 participants