2017-08-23 45 views
0

是否有模塊出口聯盟類型格式...例如:流動型聯盟類型模塊出口

// 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。 –

回答

0

您可以使用$值到對象的值轉換成聯盟類型。但是,請注意,將字符串分配給常量仍將該值輸入爲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){ ... }