流动型联盟类型模块出口

问题描述:

是否有模块出口联盟类型格式...例如:流动型联盟类型模块出口

// 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){ ... } 
+0

是否有你需要做的是在导入文件中的原因是什么?我通常只是在定义动作类型的模块中创建union类型:'export type ActionTypes ='blah'| “foo'''。 – Adam

+0

我已经有'actionTypes.js'文件中的字符串了,所以我想我可以重用FTW。 –

您可以使用$值到对象的值转换成联盟类型。但是,请注意,将字符串分配给常量仍将该值输入为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){ ... }