Skip to content

How to define Typescript types for action creators with multiple arguments? #1145

Answered by phryneas
uhef asked this question in Q&A
Discussion options

You must be logged in to vote
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…

Replies: 2 comments 4 replies

Comment options

You must be logged in to vote
2 replies
@phryneas
Comment options

@markerikson
Comment options

Comment options

You must be logged in to vote
2 replies
@uhef
Comment options

@phryneas
Comment options

Answer selected by uhef
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants