Skip to content

Instantly share code, notes, and snippets.

@andiwinata
Created November 26, 2019 02:30
Show Gist options
  • Save andiwinata/19e8139aa51ad7130ecb1ac4d138701b to your computer and use it in GitHub Desktop.
Save andiwinata/19e8139aa51ad7130ecb1ac4d138701b to your computer and use it in GitHub Desktop.
import { Action } from 'redux';
import * as plansActions from './plans/actions';
import * as planDetailsActions from './planDetails/actions';
import { reducers } from './reducer';
export type StoreState = Readonly<{ [R in keyof typeof reducers]: ReturnType<typeof reducers[R]> }>;
// from object containing { [actionName]: [action] }
// then for every action, if the action itself extending AnyAction -> append it to the object
// otherwise, if the action is a function and returning AnyAction -> append it to the object
// else just discard the action since they are ThunkAction (they return a function)
type ExtractAction<T extends { [key: string]: any }> = {
[A in keyof T]: T[A] extends Action
? T[A]
: ReturnType<T[A]> extends Action
? ReturnType<T[A]>
: never;
};
// make union of types from object values
type Values<T> = T[keyof T];
export type PlansActions = Values<ExtractAction<typeof plansActions>>;
export type PlanDetailsActions = Values<ExtractAction<typeof planDetailsActions>>;
export type StoreAction =
| PlansActions
| PlanDetailsActions
// plan actions examples:
export const planListRetrievedAction = (plans: PlanMonitorDto[]) => ({
type: PLAN_LIST_RETRIEVED,
payload: plans,
});
export const planRetrievedAction = (plan: PlanMonitorDto) => ({
type: PLAN_RETRIEVED,
payload: plan,
});
export const planStartedAction = (plan: PlanMonitorDto) => ({
type: PLAN_STARTED,
payload: plan,
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment