diff --git a/lib/utils/action-utils.js b/lib/utils/action-utils.js --- a/lib/utils/action-utils.js +++ b/lib/utils/action-utils.js @@ -32,23 +32,31 @@ let nextPromiseIndex = 0; -export type ActionTypes = { - started: AT, - success: BT, - failed: CT, +export type ActionTypes< + STARTED_ACTION_TYPE: string, + SUCCESS_ACTION_TYPE: string, + FAILED_ACTION_TYPE: string, +> = { + started: STARTED_ACTION_TYPE, + success: SUCCESS_ACTION_TYPE, + failed: FAILED_ACTION_TYPE, }; function wrapActionPromise< - AT: string, // *_STARTED action type (string literal) - AP: ActionPayload, // *_STARTED payload - BT: string, // *_SUCCESS action type (string literal) - BP: ActionPayload, // *_SUCCESS payload - CT: string, // *_FAILED action type (string literal) + STARTED_ACTION_TYPE: string, + STARTED_PAYLOAD: ActionPayload, + SUCCESS_ACTION_TYPE: string, + SUCCESS_PAYLOAD: ActionPayload, + FAILED_ACTION_TYPE: string, >( - actionTypes: ActionTypes, - promise: Promise, + actionTypes: ActionTypes< + STARTED_ACTION_TYPE, + SUCCESS_ACTION_TYPE, + FAILED_ACTION_TYPE, + >, + promise: Promise, loadingOptions: ?LoadingOptions, - startingPayload: ?AP, + startingPayload: ?STARTED_PAYLOAD, ): PromisedAction { const loadingInfo: LoadingInfo = { fetchIndex: nextPromiseIndex++, @@ -63,26 +71,26 @@ return async (dispatch: Dispatch): Promise => { const startAction = startingPayload ? { - type: (actionTypes.started: AT), + type: (actionTypes.started: STARTED_ACTION_TYPE), loadingInfo, - payload: (startingPayload: AP), + payload: (startingPayload: STARTED_PAYLOAD), } : { - type: (actionTypes.started: AT), + type: (actionTypes.started: STARTED_ACTION_TYPE), loadingInfo, }; dispatch(startAction); try { const result = await promise; dispatch({ - type: (actionTypes.success: BT), - payload: (result: BP), + type: (actionTypes.success: SUCCESS_ACTION_TYPE), + payload: (result: SUCCESS_PAYLOAD), loadingInfo, }); } catch (e) { console.log(e); dispatch({ - type: (actionTypes.failed: CT), + type: (actionTypes.failed: FAILED_ACTION_TYPE), error: true, payload: (e: Error), loadingInfo, @@ -92,18 +100,18 @@ } export type DispatchActionPromise = < - A: BaseAction, - B: BaseAction, - C: BaseAction, + STARTED: BaseAction, + SUCCESS: BaseAction, + FAILED: BaseAction, >( actionTypes: ActionTypes< - $PropertyType, - $PropertyType, - $PropertyType, + $PropertyType, + $PropertyType, + $PropertyType, >, - promise: Promise<$PropertyType>, + promise: Promise<$PropertyType>, loadingOptions?: LoadingOptions, - startingPayload?: $PropertyType, + startingPayload?: $PropertyType, ) => Promise; function useDispatchActionPromise(): DispatchActionPromise { @@ -113,18 +121,18 @@ function createDispatchActionPromise(dispatch: Dispatch) { const dispatchActionPromise = function < - A: BaseAction, - B: BaseAction, - C: BaseAction, + STARTED: BaseAction, + SUCCESS: BaseAction, + FAILED: BaseAction, >( actionTypes: ActionTypes< - $PropertyType, - $PropertyType, - $PropertyType, + $PropertyType, + $PropertyType, + $PropertyType, >, - promise: Promise<$PropertyType>, + promise: Promise<$PropertyType>, loadingOptions?: LoadingOptions, - startingPayload?: $PropertyType, + startingPayload?: $PropertyType, ): Promise { return dispatch( wrapActionPromise(actionTypes, promise, loadingOptions, startingPayload),