Skip to content

Commit

Permalink
fix the weird type hacks in the test
Browse files Browse the repository at this point in the history
  • Loading branch information
cellog committed Aug 29, 2019
1 parent 8ca8290 commit 7e7395f
Showing 1 changed file with 49 additions and 9 deletions.
58 changes: 49 additions & 9 deletions test/typescript/enhancers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,25 @@ const reducer: Reducer<State> = null as any
function dispatchExtension() {
type PromiseDispatch = <T extends Action>(promise: Promise<T>) => Promise<T>

const enhancer: StoreEnhancer<{ dispatch: PromiseDispatch }> = null as any
const enhancer: StoreEnhancer<{
dispatch: PromiseDispatch
}> = createStore => <S, A extends Action = AnyAction>(
reducer: Reducer<S, A>,
preloadedState?: any
) => {
const store = createStore(reducer, preloadedState)
return {
...store,
dispatch: (action: any) => {
if (action.type) {
store.dispatch(action)
} else if (action.then) {
action.then(store.dispatch)
}
return action
}
}
}

const store = createStore(reducer, enhancer)

Expand All @@ -37,10 +55,21 @@ function stateExtension() {
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: Reducer<S & ExtraState, A> = (state, action) => {
const newState = reducer(state, action)
return {
...newState,
extraField: 'extra'
}
}
const wrappedPreloadedState = preloadedState
? {
...preloadedState,
extraField: 'extra'
}
: undefined
return createStore(wrappedReducer, wrappedPreloadedState)
}

Expand Down Expand Up @@ -79,10 +108,21 @@ function replaceReducerExtender() {
ExtraState
> = createStore => <S, 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: Reducer<S & ExtraState, A> = (state, action) => {
const newState = reducer(state, action)
return {
...newState,
extraField: 'extra'
}
}
const wrappedPreloadedState = preloadedState
? {
...preloadedState,
extraField: 'extra'
}
: undefined
return createStore(wrappedReducer, wrappedPreloadedState)
}

Expand Down Expand Up @@ -127,12 +167,12 @@ function mhelmersonExample() {
reducer: Reducer<S, A>,
preloadedState?: any
) => {
const wrappedReducer = (state: S & ExtraState | undefined, action: A) => {
const wrappedReducer: Reducer<S & ExtraState, A> = (state, action) => {
const newState = reducer(state, action)
return {
...newState,
extraField: 'extra'
} as S & ExtraState
}
}
const wrappedPreloadedState = preloadedState
? {
Expand Down

0 comments on commit 7e7395f

Please sign in to comment.