Created
September 14, 2021 02:06
-
-
Save lichenbuliren/80389c008481a3dc7d49c91cfc52bdfa to your computer and use it in GitHub Desktop.
dynamic create reducer actions by enum action type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type CreateActionMap<M extends { [index: string]: any }> = { | |
// 通过 extends undefined 来判断是否有 payload 要求 | |
[Key in keyof M]: M[Key] extends undefined | |
? { | |
type: Key; | |
} | |
: { | |
type: Key; | |
payload: M[Key]; | |
}; | |
}; | |
export enum UserActionTypes { | |
UPDATE_LIST_KEYS = 'listKeys', | |
UPDATE_BTN_ACTION_MAP = 'updateActionButtonInfoMap', | |
UPDATE_USER_INFO = 'updateUserInfo', | |
UPDATE_BTN_TEXT_BY_USER_STATUS = 'updateBtnTextByUserStatus', | |
UPDATE_UNLOCK_BTN_TEXT = 'updateUnLockBtnText', | |
UPDATE_BTN_ACTION_TYPE = 'updateBtnActionType', | |
} | |
export type ActionPayload = { | |
[UserActionTypes.UPDATE_USER_INFO]: Partial<BaseInfo>; | |
[UserActionTypes.UPDATE_LIST_KEYS]: BaseInfoKeys[]; | |
[UserActionTypes.UPDATE_BTN_ACTION_MAP]: Partial<BtnActionMap>; | |
[UserActionTypes.UPDATE_BTN_TEXT_BY_USER_STATUS]: UserStatusType; | |
[UserActionTypes.UPDATE_BTN_ACTION_TYPE]: ActionType; | |
}; | |
export type SearchUserAction = | |
CreateActionMap<ActionPayload>[keyof ActionPayload]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment