Using reducers
in my slice instance ( redux/toolkit ) I get this error.
'reducers' does not exist in type 'CreateSliceOptions<unknown, SliceCaseReducers<unknown>, string>'.ts(2345)
Instead of reducers, addPermissions exits in type (CreateSliceOptions).
I guess something might have gone wrong with my typescript types
code:
import { createSlice } from "@reduxjs/toolkit";
interface IPermission {}
interface IState {
userPermissions: IPermission[];
}
export const initialState: IState = {
userPermissions: localStorage.getItem("token") ? [] : [],
};
export const permissionSlice = createSlice({
name: "permissions",
initialState,
reducers: {
addPermission: (state: any, action: any) => {
state.userPermissions = action.payload;
},
},
});
export const { addPermission } = permissionSlice.actions;
export default permissionSlice.reducer;
CodePudding user response:
In your code, the type of the slice state is IState, so you should update the type of the reducers field to be SliceCaseReducers.
import { createSlice, SliceCaseReducers } from "@reduxjs/toolkit";
interface IPermission {}
interface IState {
userPermissions: IPermission[];
}
export const initialState: IState = {
userPermissions: localStorage.getItem("token") ? [] : [],
};
export const permissionSlice = createSlice({
name: "permissions",
initialState,
reducers: {
addPermission: (state: IState, action: any) => {
state.userPermissions = action.payload;
},
} as SliceCaseReducers<IState>,
});
export const { addPermission } = permissionSlice.actions;
export default permissionSlice.reducer;
CodePudding user response:
try as
to consider the type as SliceCaseReducers<IState>
reducers: {
addPermission: (state: IState, action: any) => {
state.userPermissions = action.payload;
},
} as SliceCaseReducers<IState>,