-
|
This is probably straight forward, but I've been lost in reading Typescript type definitions of Problem is that I've introduced a reducer with 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.
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.
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…