具有所需负载的 createAction

createAction with required payload

您能否举例说明如何使用所需的负载创建操作?

例如,如果我这样创建

const foo = createAction<{ email: string }>('some_type');

foo() 的类型等于 ActionCreatorWithOptionalPayload 如何用ActionCreatorWithPayload得到它?

gh 问题 link:https://github.com/reduxjs/redux-toolkit/issues/834

在这里重复我的回答以防其他人看到:

我 99% 确定您在 tsconfig.json.

中使用带有 strictNullChecks: false 的 TypeScript

这意味着您的代码由 TypeScript 处理,例如 const foo = createAction<{ email: string } | null | undefined>('some_type'); - undefined 被 RTK 解释为“这是可选的”。

如果你有strictNullChecks: false,你所做的每个类型定义在内部总是| null | undefined。如果您不相信我,请尝试像 const x: number = nullconst y: number = undefined 这样的代码。这实际上不是 TS 的使用方式,而只是 TS pre-2.3 的“向后兼容性”功能。

除了您为 TypeScript 配置启用严格模式外,不幸的是我们对此无能为力。