最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to type 'prepare' function in redux-toolkit - Stack Overflow

programmeradmin3浏览0评论

I am using redux-toolkit prepare function to construct the final payload value.

    addTodo: {
      reducer: (state, action) => {
        state.push(action.payload);
      },
      // ERROR: **Type '{ payload: Todo; }' is missing the following properties from type 'Omit<PayloadAction<any, string, any, any>, "type">': meta, errorts**
      prepare: (todoMessage: string): { payload: Todo } => {
        return {
          payload: { message: todoMessage, id: uuid(), pleted: false }
        };
      }
    },

How can I type prepare function to remove the typescript error?

Check the error here.

I am using redux-toolkit prepare function to construct the final payload value.

    addTodo: {
      reducer: (state, action) => {
        state.push(action.payload);
      },
      // ERROR: **Type '{ payload: Todo; }' is missing the following properties from type 'Omit<PayloadAction<any, string, any, any>, "type">': meta, errorts**
      prepare: (todoMessage: string): { payload: Todo } => {
        return {
          payload: { message: todoMessage, id: uuid(), pleted: false }
        };
      }
    },

How can I type prepare function to remove the typescript error?

Check the error here.

Share Improve this question asked Jun 3, 2022 at 7:25 RashomonRashomon 6,7624 gold badges38 silver badges79 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 17
    addTodo: {
      reducer: (state, action: PayloadAction<Todo>) => {
        state.push(action.payload);
      },
      prepare: (todoMessage: string) => {
        return {
          payload: { message: todoMessage, id: uuid(), pleted: false }
        };
      }
    },

you just need to add a payload type on the action.

发布评论

评论列表(0)

  1. 暂无评论