Here is my implementation of the createSlice() function:
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
type TransferDeckModeType = "pipetting" | "evaluation" | "editing";
var initialState: TransferDeckModeType = "pipetting";
const transfer_deck_mode = createSlice({
name: "transfer_deck_mode",
initialState,
reducers: {
setTransferDeckMode(state, action: PayloadAction<TransferDeckModeType>) {
let newState = action.payload;
return newState;
},
},
});
export const { setTransferDeckMode } = transfer_deck_mode.actions;
export default transfer_deck_mode.reducer;
An issue arises with the setTransferDeckMode
function in Typescript.
Type '(state: "pipetting", action: { payload: TransferDeckModeType; type: string; }) => TransferDeckModeType' is not assignable to type 'CaseReducer<"pipetting", { payload: any; type: string; }> | CaseReducerWithPrepare<"pipetting", PayloadAction<any, string, any, any>>'.
Type '(state: "pipetting", action: { payload: TransferDeckModeType; type: string; }) => TransferDeckModeType' is not assignable to type 'CaseReducer<"pipetting", { payload: any; type: string; }>'.
Type 'TransferDeckModeType' is not assignable to type 'void | "pipetting"'.
Type '"evaluation"' is not assignable to type 'void | "pipetting"'.ts(2322)
(method) setTransferDeckMode(state: "pipetting", action: PayloadAction<TransferDeckModeType>): TransferDeckModeType
The unexpected presence of 'void' raises questions about the state declaration. What is introducing 'void' and setting it alongside ""pipetting""? It's puzzling as the state should not be void in this context.
There seems to be confusion on where to specify the state of the slice. Shouldn't it be inferred from the type of initialState? This oversight is causing confusion.
Interestingly, changing the type of initialState to string
resolves the error.
var initialState: string = "pipetting";
However, this solution feels inadequate. Why can't I define the type of the initialState directly?