Skip to content

Commit

Permalink
fix replaceReducer, so that it infers types, fix example test
Browse files Browse the repository at this point in the history
  • Loading branch information
cellog committed Aug 29, 2019
1 parent 8ed803d commit 8ca8290
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ export interface Store<
*
* @param nextReducer The reducer for the store to use instead.
*/
replaceReducer<NewState = S, NewActions extends A = A>(
replaceReducer<NewState, NewActions extends Action>(
nextReducer: Reducer<NewState, NewActions>
): Store<NewState & StateExt, NewActions, StateExt, Ext> & Ext

Expand Down
39 changes: 32 additions & 7 deletions test/typescript/enhancers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PreloadedState, Store } from '../../index'
import { PreloadedState } from '../../index'
import { StoreEnhancer, Action, AnyAction, Reducer, createStore } from 'redux'

interface State {
Expand Down Expand Up @@ -125,18 +125,43 @@ function mhelmersonExample() {
A extends Action = AnyAction
>(
reducer: Reducer<S, A>,
preloadedState?: PreloadedState<S>
preloadedState?: any
) => {
const wrappedReducer: Reducer<S & ExtraState, A> = null as any
const wrappedPreloadedState: PreloadedState<S & ExtraState> = null as any
const wrappedReducer = (state: S & ExtraState | undefined, action: A) => {
const newState = reducer(state, action)
return {
...newState,
extraField: 'extra'
} as S & ExtraState
}
const wrappedPreloadedState = preloadedState
? {
...preloadedState,
extraField: 'extra'
}
: undefined
const store = createStore(wrappedReducer, wrappedPreloadedState)
return {
...store,
replaceReducer: (nextReducer: Reducer<S, A>) => {
const nextWrappedReducer: Reducer<S & ExtraState, A> = null as any
replaceReducer<NS, NA extends Action = AnyAction>(
nextReducer: (
state: NS & ExtraState | undefined,
action: NA
) => NS & ExtraState
) {
const nextWrappedReducer: Reducer<NS & ExtraState, NA> = (
state,
action
) => {
const newState = nextReducer(state, action)
return {
...newState,
extraField: 'extra'
}
}
return store.replaceReducer(nextWrappedReducer)
}
} as Store<S & ExtraState, A, ExtraState>
}
}

const store = createStore(reducer, enhancer)
Expand Down

0 comments on commit 8ca8290

Please sign in to comment.