我被困在这个问题大约3天。 简化这样的例子。 当我添加" custom"动作类型并得到错误"未捕获的TypeError:action.custom必须是一个字符串" 相同的代码适用于使用react-redux的creat-react-app。 我可以跳过动作的注释以运行应用程序,但这个问题/警告让我困扰:(
// @flow
export const INCREMENT_COUNTER = 'INCREMENT_COUNTER';
export const DECREMENT_COUNTER = 'DECREMENT_COUNTER';
export type counterStateType = {
+counter: number
};
type actionType = {
+type: string,
+custom: string
};
export default function counter(state: number = 0, action: actionType) {
switch (action.type) {
case INCREMENT_COUNTER:
return state + 1;
case DECREMENT_COUNTER:
return state - 1;
default:
return state;
}
}

combineReducers.js:109 Uncaught TypeError: action.custom must be a string
Expected: string
Actual Value: undefined
Actual Type: void
at counter (http://localhost:1212/dist/renderer.dev.js:2419:54)
at eval (webpack:///./node_modules/redux/es/combineReducers.js?:45:24)
at Array.forEach (native)
at assertReducerShape (webpack:///./node_modules/redux/es/combineReducers.js?:43:25)
at combineReducers (webpack:///./node_modules/redux/es/combineReducers.js?:99:5)
at Object.<anonymous> (http://localhost:1212/dist/renderer.dev.js:2493:48)
at Object../app/reducers/index.js (http://localhost:1212/dist/renderer.dev.js:2521:30)
at __webpack_require__ (http://localhost:1212/dist/renderer.dev.js:680:30)
at fn (http://localhost:1212/dist/renderer.dev.js:90:20)
at Object.<anonymous> (http://localhost:1212/dist/renderer.dev.js:2969:17)
at Object../app/store/configureStore.dev.js (http://localhost:1212/dist/renderer.dev.js:3085:30)
at __webpack_require__ (http://localhost:1212/dist/renderer.dev.js:680:30)
at fn (http://localhost:1212/dist/renderer.dev.js:90:20)
at Object../app/store/configureStore.js (http://localhost:1212/dist/renderer.dev.js:3098:20)
at __webpack_require__ (http://localhost:1212/dist/renderer.dev.js:680:30)
at fn (http://localhost:1212/dist/renderer.dev.js:90:20)
&#13;
答案 0 :(得分:0)
您可以尝试添加问号:
actionType = {
+type: string,
+custom?: string
};
这应该允许您在有或没有&#39; custom&#39;的情况下采取行动。属性,但属性必须始终为字符串
{
type: 'DECREMENT_COUNTER',
custom: 'three
}
我正在使用
type actionType = {
type: string,
payload?: Object
};
我在Object中添加了我想要的有效负载,但是你可能会收到警告,因为Object是弱类型
希望得到这个帮助。