I am encountering an issue when trying to add a type to action.payload in the Redux Toolkit.
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
interface InputState {
user: {
firstName: string;
lastName: string | null;
userID: number | null;
};
}
const initialState: InputState = {
user: {
firstName: "individual",
lastName: null,
userID: null,
},
};
export const inputsDataSlice = createSlice({
name: "inputsData",
initialState,
reducers: {
setValue: (state, action: PayloadAction<string | number | null>) => {
const data: string = action.payload;
switch (data.id) {
case "firstName":
return void (state.user.firstName = data.value);
case "lastName":
return void (state.user.lastName = data.value);
case "userID":
return void (state.user.userID = data.value);
default:
return;
}
},
},
});
export const { setValue } = inputsDataSlice.actions;
export default inputsDataSlice.reducer;
The error message 'Property 'value' / 'id' does not exist on type 'string'' is still appearing for data.id and data.value. I have specified the types as <string | number | null>
with PayloadAction, so where could the mistake be located?
If you could provide any guidance on this issue, it would be greatly appreciated. Thank you!