1

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

以下是我的操作代码:

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);

下面是我的减速器:

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
);

问题是关于payload里面的类型handleActions。它的类型是ProductActionProps | undefined. 我必须检查是否payloadundefined函数体内。但我不明白为什么类型接受undefined

4

1 回答 1

0

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

如果您还没有,请安装redux-actions类型声明。

npm install @types/redux-actions --save-dev

然后重启你的编辑器(为了重启 TypeScript 语言服务)。

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

于 2019-06-11T19:18:29.997 回答