使用Redux键入操作的最佳方法? (流动型)

时间:2017-11-30 21:25:29

标签: redux flowtype

使用redux键入操作的最佳方法是什么?

“最佳”意味着最大限度地提高可读性,同时最大限度地减少编写/维护所需的工作。

e.g。对于文件......

// @flow

export const incrementCounter = (counter: string, amount: number) => ({
  type: 'INCREMENT_COUNTER',
  counter,
  amount
})

export const resetCounter = (counter: string) => ({
  type: 'RESET_COUNTER',
  counter
})

1 个答案:

答案 0 :(得分:1)

这是我目前输入操作的方式,它使用$Call Utility Type

// @flow

export const incrementCounter = (counter: string, amount: number) => ({
  type: 'INCREMENT_COUNTER',
  counter,
  amount
})

export const resetCounter = (counter: string) => ({
  type: 'RESET_COUNTER',
  counter
})

export Action = 
  | $Call<typeof incrementCounter, *, *>
  | $Call<typeof resetCounter, *>

如果您将操作拆分为多个文件,则可以将所有操作合并到顶级索引中,如此...

import type { Action as CounterActions } from './counter-actions'
import type { Action as TimerActions } from './timer-actions'

export type Action =
  | CounterActions
  | TimerActions