Hello I have a problem I get an error as in title - expresion of type string cant be used to index type 'WritableDraft'. I would like to make it dynamic to avoid many "if" statements. Why it does not work state[action.payload.filterName]
after updating type definition, filterName: keyOf InitialState I get an error
Property 'push' does not exist on type 'string | string[] | WritableDraft<Recipe>[] | WritableDraft<{ recipes: Recipe[]; totalAmount: number; }>'.
Property 'push' does not exist on type 'string'.
in a line:
addFilters(state, action: PayloadAction<FilteringConfiguration>) {
state[action.payload.nameOfFilter].push(action.payload.content);
},
filtering type:
export type FilteringConfiguration = {
content: string;
type: typeOfFiltering;
set: boolean;
filterName: key of InitialState;
};
component:
const onChangeHandler = (e: React.ChangeEvent<HTMLInputElement>) => {
const content = e.target.value;
dispatch(
recipeAction.setFilters({
type: props.type,
set: e.target.checked,
content: content,
filterName: "filterTypes",
})
);
};
redux toolkit
const INITIAL_VALUE: InitialState = {
recipes: [],
likedRecipes: {
recipes: [],
totalAmount: 0,
},
recipeTypes: ["Breakfast", "Lunch", "Dinner", "Supper"],
recipeTime: [
"Very short (~30min)",
"short (~1hr)",
"medium (~3hrs)",
"Long (~6hrs)",
],
// variables for filtering recipes
recipeTitle: "",
filterTypes: [],
filterLengths: [],
};
const recipeSlice = createSlice({
name: "recipe",
initialState: INITIAL_VALUE,
reducers: {
addFilters(state, action: PayloadAction<FilteringConfiguration>) {
state[action.payload.filterName].push(action.payload.content);
}, // there is an error