首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我的reducer参数类型接受`unfined`?

为什么我的reducer参数类型接受`unfined`?
EN

Stack Overflow用户
提问于 2019-06-11 19:58:04
回答 1查看 129关注 0票数 1

我在我的react/redux应用中使用typescript。有一个我不明白的类型错误。

下面是我的操作代码:

代码语言:javascript
复制
import { createAction } from 'redux-actions';

export enum ProductActionType {
  SEARCH_STAMP = 'SEARCH_STAMP',
  SEARCH_PRODUCT = 'SEARCH_PRODUCT',
};

export interface ProductActionProps {
  text: string;
}

const searchStamp = createAction<ProductActionProps>(ProductActionType.SEARCH_STAMP);

下面是我的reducer:

代码语言:javascript
复制
import { handleActions } from 'redux-actions';
import { ProductActionType, ProductActionProps } from '@ap-actions/index';

export const productReducer = handleActions<Product, ProductActionProps>(
  {
    [ProductActionType.SEARCH_STAMP]: (state, { payload }) => { // here why payload type is ProductActionProps | undefined?
      ...
    }
  }, initialState
);

这个问题与handleActions中的payload类型有关。其类型为ProductActionProps | undefined。我必须检查函数体中的payload是否为undefined。但是我不明白为什么这个类型接受undefined

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-12 03:18:30

您说得对,{ payload }的类型应该是{ ProductActionProps }。我想知道您为redux-actions安装的类型定义。

如果您尚未安装redux-actions类型声明,请安装它。

代码语言:javascript
复制
npm install @types/redux-actions --save-dev

然后重新启动编辑器(以便重新启动TypeScript语言服务)。

如果这仍然不起作用,您可能需要升级您的类型。我使用的是"@types/redux-actions": "^2.6.1",它不会重现你的错误。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56543198

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档