我在我的react/redux应用中使用typescript。有一个我不明白的类型错误。
下面是我的操作代码:
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:
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
发布于 2019-06-12 03:18:30
您说得对,{ payload }的类型应该是{ ProductActionProps }。我想知道您为redux-actions安装的类型定义。
如果您尚未安装redux-actions类型声明,请安装它。
npm install @types/redux-actions --save-dev然后重新启动编辑器(以便重新启动TypeScript语言服务)。
如果这仍然不起作用,您可能需要升级您的类型。我使用的是"@types/redux-actions": "^2.6.1",它不会重现你的错误。
https://stackoverflow.com/questions/56543198
复制相似问题