Replies: 2 comments 4 replies
-
Hmm. As a first issue, you shouldn't be actually specifying the generic args to https://redux-toolkit.js.org/tutorials/typescript#define-slice-state-and-action-types (also as a side note, you can "mutate" the state inside the reducer instead of returning spreads.) Similarly, your I'm actually still a bit unsure how the |
Beta Was this translation helpful? Give feedback.
-
export type TodoState = {
[keys: string]: {status: string}
}
-const slice = createSlice<TodoState, SliceCaseReducers<TodoState>, string>({
+const slice = createSlice({
name: 'todos',
- initialState: {},
+ initialState: {} as TodoState,
reducers: {
todoReceived: {
reducer(state, action: PayloadAction<string, string, never, boolean>) {
const todoId = action.payload;
if (action.error) {
return {
...state,
[todoId]: { status: 'oh-no' }
}
}
return {
...state,
[todoId]: { status 'ok' }
}
},
prepare(id: string, error: boolean) {
return {
payload: id,
error
};
}
}
},
}); something like this should work. Never assign generics for |
Beta Was this translation helpful? Give feedback.
-
This is probably straight forward, but I've been lost in reading Typescript type definitions of
createSlice
a little while now and don't seem to find a solution.Problem is that I've introduced a reducer with
prepare
function increateSlice
that takes two argument (a string and a boolean) and it seems that the action creator function generated bycreateSlice
has a type that only expects one argument. Thus the Typescript compiler whines and I would like to fix that. Ignoring ts errors does the job but I would really like to have the action creator to have correct types thus this really does not satisfy.Example:
I think I should somehow make the 'todoReceived' action creator type explicit but I have no idea how to achieve this. Any ideas?
Beta Was this translation helpful? Give feedback.
All reactions