Refactor store to accept state as part of params

The old code was returning a fn because TypeScript could not figure out
generic type parameters when params was an object like:

    interface Params<State> {
      a: Reducer<State>
      b: Partial<State>
    }

    function test<State>(params: Params<State>) {
      // ...
    }
This commit is contained in:
Jerko Steiner 2019-09-26 23:52:41 +07:00
parent 5623001497
commit a8d603dd59
4 changed files with 9 additions and 8 deletions

View File

@ -74,7 +74,7 @@ describe('CRUD', () => {
}, },
}) })
function getStore() { function getStore() {
return test.createStore({reducer})() return test.createStore({reducer})
} }
type Store = ReturnType<typeof getStore> type Store = ReturnType<typeof getStore>

View File

@ -62,12 +62,13 @@ export class TestUtils {
let store = this.createStore({ let store = this.createStore({
reducer: this.combineReducers(reducers), reducer: this.combineReducers(reducers),
extraMiddleware: [waitMiddleware.handle], extraMiddleware: [waitMiddleware.handle],
})() })
const withState = (state: DeepPartial<State>) => { const withState = (state: Partial<State>) => {
store = this.createStore({ store = this.createStore({
reducer: this.combineReducers(reducers), reducer: this.combineReducers(reducers),
})(state) state,
})
return {withComponent} return {withComponent}
} }

View File

@ -143,7 +143,7 @@ describe('createActions', () => {
}) })
const reducer = combineReducers({handler, mapping}) const reducer = combineReducers({handler, mapping})
const store = createStore({reducer})() const store = createStore({reducer})
return {client, store} return {client, store}
} }

View File

@ -1,4 +1,4 @@
import { Action, applyMiddleware, createStore as create, DeepPartial, Middleware, Reducer } from 'redux' import { Action, applyMiddleware, createStore as create, Middleware, Reducer } from 'redux'
import { PromiseMiddleware, ReduxLogger } from '../middleware' import { PromiseMiddleware, ReduxLogger } from '../middleware'
export interface CreateStoreParams<State, A extends Action> { export interface CreateStoreParams<State, A extends Action> {
@ -25,9 +25,9 @@ export function createStore<State, A extends Action>(
if (params.extraMiddleware) { if (params.extraMiddleware) {
middleware.push(...params.extraMiddleware) middleware.push(...params.extraMiddleware)
} }
return (state?: DeepPartial<State>) => create( return create(
params.reducer, params.reducer,
state, params.state,
applyMiddleware(...middleware), applyMiddleware(...middleware),
) )
} }