Skip to content

Instantly share code, notes, and snippets.

@lichenbuliren
Created September 14, 2021 02:06
Show Gist options
  • Save lichenbuliren/80389c008481a3dc7d49c91cfc52bdfa to your computer and use it in GitHub Desktop.
Save lichenbuliren/80389c008481a3dc7d49c91cfc52bdfa to your computer and use it in GitHub Desktop.
dynamic create reducer actions by enum action type
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