模块导出是否有联合类型格式...例如:
// 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){ ... }
可以使用$Values将对象的值转换为联合类型。但请注意,将字符串分配给常量仍将该值键入为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){ ... }