首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >flowtype:当在redux操作中使用常量时,“对象文字无法决定选择哪一个大小写”

flowtype:当在redux操作中使用常量时,“对象文字无法决定选择哪一个大小写”
EN

Stack Overflow用户
提问于 2017-06-10 17:37:26
回答 1查看 1.4K关注 0票数 11

编辑:显示问题的最小示例的这是一个完整的GitHub回购

我有一个简单的计数器应用程序。以下是我的行为创作者:

actions.js

代码语言:javascript
复制
/**
 * @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 }

目前,我在incrementdecrement函数中都出现了一个错误,即对象文本无法决定选择联合类型的大小写。

要修复这些错误,我可以将type: INCREMENT更改为type: 'INCREMENT',将type: DECREMENT更改为type: 'DECREMENT'。但是,我将在多个地方使用这个常量(比如还原器),所以我希望能够导入这个常量并在那里使用它。难道这不是流型的方式吗?

为了清晰起见,下面是其余的文件:

constants.js

代码语言:javascript
复制
/**
 * @flow
 */

const INCREMENT: 'INCREMENT' = 'INCREMENT'
const DECREMENT: 'DECREMENT' = 'DECREMENT'

export {
  INCREMENT,
  DECREMENT
}

reducer.js

代码语言:javascript
复制
/**
 * @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

编辑:这是一个详细的错误日志

代码语言:javascript
复制
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`
EN

回答 1

Stack Overflow用户

发布于 2017-06-13 06:22:18

尝试将typeof添加到操作声明中:

代码语言:javascript
复制
type Action = 
  | { type: typeof INCREMENT } 
  | { type: typeof DECREMENT }

试着在这里流动

您还可以使用$Keys

代码语言:javascript
复制
const ActionTypes = {
  INCREMENT: 'INCREMENT',
  DECREMENT: 'DECREMENT'
}

type Action = { type: $Keys<typeof ActionTypes> } 

这看起来不那么冗长

更多信息在这里:https://github.com/facebook/flow/issues/2377

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44476216

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档