Skip to content

Instantly share code, notes, and snippets.

@cellog
Created September 2, 2019 15:41
Show Gist options
  • Save cellog/7f387bf7b5038adce3e6f8ad7b4f54f6 to your computer and use it in GitHub Desktop.
Save cellog/7f387bf7b5038adce3e6f8ad7b4f54f6 to your computer and use it in GitHub Desktop.
Better types, with inference
interface Action<T = any> { type: T }
interface AnyAction extends Action { [extraProps: string]: any }
type Reducer<S = any, A extends Action = AnyAction> = (
state: S | undefined,
action: A
) => S
interface Store<
S = any,
A extends Action = AnyAction,
StateExt = BaseType<S>,
Ext = {}
> { {
getState(): S & StateExt
dispatch(action: A): A
replaceReducer<NewState, NewActions extends Action>(
nextReducer: Reducer<NewState, NewActions>
): Store<NewState & StateExt, NewActions, StateExt, Ext> & Ext
}
export type StoreEnhancer<StoreExt = {}, StateExt = {}> = (
next: StoreEnhancerStoreCreator<StoreExt, StateExt>
) => StoreEnhancerStoreCreator<StoreExt, StateExt>
export type StoreEnhancerStoreCreator<StoreExt = {}, StateExt = {}> = <
S = any,
A extends Action = AnyAction
>(
reducer: Reducer<S, A>,
preloadedState?: S
) => Store<S & StateExt, A, StateExt, Ext> & StoreExt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment