我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
. 我必须检查是否payload
在undefined
函数体内。但我不明白为什么类型接受undefined
?