[英]flowtype: “object literal could not decide which case to select” when using constants in redux actions
EDIT : Here is a full GitHub repo of a minimal example that shows the issue. 编辑 : 这是一个完整的GitHub回购的最小示例,显示了该问题。
I have a simple Counter app. 我有一个简单的Counter应用程序。 Here are my action creators: 以下是我的动作创作者:
actions.js actions.js
/**
* @flow
*/
import { INCREMENT, DECREMENT } from '../constants'
type Action =
| { type: 'INCREMENT' }
| { type: 'DECREMENT' }
function increment(): Action {
return {
type: INCREMENT
}
}
function decrement(): Action {
return {
type: DECREMENT
}
}
export { increment, decrement }
export type { Action }
Currently, I'm getting an error in both the increment
and decrement
functions which states that object literal Could not decide which case to select union type . 目前,我在increment
和decrement
函数中都收到错误,这些函数声明对象文字无法决定选择联合类型的情况 。
To fix these errors, I can change type: INCREMENT
to type: 'INCREMENT'
and change type: DECREMENT
to type: 'DECREMENT'
. 要修复这些错误,我可以更改type: INCREMENT
type: 'INCREMENT'
并更改type: DECREMENT
type: 'DECREMENT'
。 However, I'm going to be using this constant in multiple places (like the reducer), so I was hoping to be able to just import the constant and use it there. 但是,我将在多个地方使用这个常量(比如reducer),所以我希望能够导入常量并在那里使用它。 Is this not the way it's done in flowtype? 这不是它在flowtype中完成的方式吗?
For clarity, here are the rest of the files: 为清楚起见,以下是其余文件:
constants.js constants.js
/**
* @flow
*/
const INCREMENT: 'INCREMENT' = 'INCREMENT'
const DECREMENT: 'DECREMENT' = 'DECREMENT'
export {
INCREMENT,
DECREMENT
}
reducer.js reducer.js
/**
* @flow
*/
import { INCREMENT, DECREMENT } from '../constants'
import type { Action } from '../actions'
type State = number
function counter(state: State = 0, action: Action): State {
switch (action.type) {
case INCREMENT:
return state + 1
case DECREMENT:
return state - 1
default:
return state
}
}
export default counter
Edit: Here is a detailed error log 编辑:这是一个详细的错误日志
src/actions/counter.js:12
v
12: return {
13: type: INCREMENT
14: }
^ object literal. Could not decide which case to select
11: function increment(): Action {
^^^^^^ union type
Case 1 may work:
8: | { type: 'INCREMENT' }
^^^^^^^^^^^^^^^^^^^^^ object type
But if it doesn't, case 2 looks promising too:
9: | { type: 'DECREMENT' }
^^^^^^^^^^^^^^^^^^^^^ object type
Please provide additional annotation(s) to determine whether case 1 works (or consider merging it with case 2):
13: type: INCREMENT
^^^^^^^^^ identifier `INCREMENT`
src/actions/counter.js:18
v
18: return {
19: type: DECREMENT
20: }
^ object literal. Could not decide which case to select
17: function decrement(): Action {
^^^^^^ union type
Case 1 may work:
8: | { type: 'INCREMENT' }
^^^^^^^^^^^^^^^^^^^^^ object type
But if it doesn't, case 2 looks promising too:
9: | { type: 'DECREMENT' }
^^^^^^^^^^^^^^^^^^^^^ object type
Please provide additional annotation(s) to determine whether case 1 works (or consider merging it with case 2):
19: type: DECREMENT
^^^^^^^^^ identifier `DECREMENT`
Try to add typeof
into you action declaration: 尝试将typeof
添加到您的动作声明中:
type Action =
| { type: typeof INCREMENT }
| { type: typeof DECREMENT }
Also you can use $Keys
您也可以使用$Keys
const ActionTypes = {
INCREMENT: 'INCREMENT',
DECREMENT: 'DECREMENT'
}
type Action = { type: $Keys<typeof ActionTypes> }
this looks less verbose 这看起来不那么冗长
additional info here: https://github.com/facebook/flow/issues/2377 其他信息: https : //github.com/facebook/flow/issues/2377
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.