简体   繁体   English

如何在 redux-toolkit 中正确使用带有元类型的 PayloadAction?

[英]How to properly use PayloadAction with meta type in redux-toolkit?

Simplified example简化示例

 import { createSlice, PayloadAction } from '@reduxjs/toolkit'; type Cake = { flavor: string; size: 'S' | 'M' | 'L'; }; const initialState: { all: Cake[]; meta: { currentPage: number } } = { all: [], meta: { currentPage: 0, }, }; const cakeSlice = createSlice({ name: 'cake', initialState, reducers: { fetchAll( state, action: PayloadAction<Cake[], string, { currentPage: number }>, ) { state.all = action.payload; state.meta = action.meta; }, }, }); export default cakeSlice;

I am getting these errors.我收到这些错误。

Type '
(state: {
    all: {
        flavor: string;size: "S" | "M" | "L";
    } [];meta: {
        currentPage: number;
    };
}, action: PayloadAction < Cake[], string, {
    currentPage: number;
}, never > ) => void ' is not assignable to type '
CaseReducer < {
    all: Cake[];meta: {
        currentPage: number;
    };
}, {
    payload: any;type: string;
} > | CaseReducerWithPrepare < {
    all: Cake[];meta: {
        currentPage: number;
    };
}, {
    payload: any;type: string;
} > '
Type '
(state: {
    all: {
        flavor: string;size: "S" | "M" | "L";
    } [];meta: {
        currentPage: number;
    };
}, action: PayloadAction < Cake[], string, {
    currentPage: number;
}, never > ) => void '
is not assignable to type 'CaseReducer<{ all: Cake[]; meta: { currentPage: number; }; }, { payload: any; type: string; }>'.
Types of parameters 'action'
and 'action'
are incompatible.
Type '{ payload: any; type: string; }'
is not assignable to type 'PayloadAction<Cake[], string, { currentPage: number; }, never>'.
Property 'meta'
is missing in type '{ payload: any; type: string; }'
but required in type '{ meta: { currentPage: number; }; }
'.

The default behaviour of RTK is to create an action creator with only one argument and only a payload property. RTK 的默认行为是创建一个只有一个参数和一个有效载荷属性的动作创建者。 This behaviour cannot be modified by TS annotations, because TS annotations have no effect on runtime behaviour. TS 注释无法修改此行为,因为 TS 注释对运行时行为没有影响。

You can use the prepare notation to define a function that overrides this default behaviour.您可以使用prepare符号来定义覆盖此默认行为的函数。 This function has the same signature & behaviour as the prepare argument for createAction , so you can find most of the relevant documentation there.此函数与createActionprepare参数具有相同的签名和行为,因此您可以在那里找到大部分相关文档。

In your specific case, you'd want something along the lines of在您的特定情况下,您会想要一些类似的东西

const cakeSlice = createSlice({
  name: 'cake',
  initialState,
  reducers: {
    fetchAll: {
      reducer(
        state,
        action: PayloadAction<Cake[], string, { currentPage: number }>
      ) {
        state.all = action.payload
        state.meta = action.meta
      },
      prepare(payload: Cake[], currentPage: number) {
        return { payload, meta: { currentPage } }
      }
    }
  }
})

Here it is as a typescript playground这是一个打字稿游乐场

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM