用于模块导出的Flow-type Union-type

时间:2017-08-23 20:37:55

标签: javascript module ecmascript-6 flowtype es6-modules

模块导出是否有Union类型格式...例如:

// actionTypes.js
export const CREATE_ACCOUNT = 'CREATE_ACCOUNT'
export const UPDATE_ACCOUNT = 'UPDATE_ACCOUNT'
export const DELETE_ACCOUNT = 'DELETE_ACCOUNT'

// reducer.js
import * as actionTypes from './actionTypes.js'

type Action = actionTypes
export default function( state: Object, action: Action){ ... }

1 个答案:

答案 0 :(得分:0)

您可以使用$ Values将对象的值转换为Union类型。但请注意,将字符串分配给const仍会将该值键入string。您需要明确键入每个变量,或使用类似$ObjMap的内容。

一个例子可能是:

// actionTypes.js
export const CREATE_ACCOUNT: 'CREATE_ACCOUNT' = 'CREATE_ACCOUNT'
export const UPDATE_ACCOUNT: 'UPDATE_ACCOUNT' = 'UPDATE_ACCOUNT'
export const DELETE_ACCOUNT: 'DELETE_ACCOUNT' = 'DELETE_ACCOUNT'

// reducer.js
import * as actionTypes from './actionTypes.js'

type Action = $Values<typeof actionTypes>;
export default function( state: Object, action: Action){ ... }