Skip to content

Commit 2c1af36

Browse files
cellogtimdorr
andcommitted
fix replaceReducer type (#3507)
* fix replaceReducer type * remove extraneous type * Update the JSDoc so IDEs don't complain. * new test for replaceReducers Co-authored-by: Tim Dorr <[email protected]>
1 parent 577dd90 commit 2c1af36

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

index.d.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,9 @@ export interface Store<S = any, A extends Action = AnyAction> {
326326
*
327327
* @param nextReducer The reducer for the store to use instead.
328328
*/
329-
replaceReducer(nextReducer: Reducer<S, A>): void
329+
replaceReducer<NewState = S, NewActions extends A = A>(
330+
nextReducer: Reducer<NewState, NewActions>
331+
): Store<NewState, NewActions>
330332

331333
/**
332334
* Interoperability point for observable/reactive libraries.

src/createStore.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ export default function createStore(reducer, preloadedState, enhancer) {
224224
* implement a hot reloading mechanism for Redux.
225225
*
226226
* @param {Function} nextReducer The reducer for the store to use instead.
227-
* @returns {void}
227+
* @returns {Store} The same store instance with a new reducer in place.
228228
*/
229229
function replaceReducer(nextReducer) {
230230
if (typeof nextReducer !== 'function') {
@@ -238,6 +238,7 @@ export default function createStore(reducer, preloadedState, enhancer) {
238238
// will receive the previous state. This effectively populates
239239
// the new state tree with any relevant data from the old one.
240240
dispatch({ type: ActionTypes.REPLACE })
241+
return store
241242
}
242243

243244
/**
@@ -284,11 +285,12 @@ export default function createStore(reducer, preloadedState, enhancer) {
284285
// the initial state tree.
285286
dispatch({ type: ActionTypes.INIT })
286287

287-
return {
288+
const store = {
288289
dispatch,
289290
subscribe,
290291
getState,
291292
replaceReducer,
292293
[$$observable]: observable
293294
}
295+
return store
294296
}

test/replaceReducers.spec.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { createStore, combineReducers } from '..'
2+
3+
describe('replaceReducers test', () => {
4+
it('returns the original store', () => {
5+
const nextReducer = combineReducers({
6+
foo: (state = 1, _action) => state,
7+
bar: (state = 2, _action) => state
8+
})
9+
const store = createStore((state, action) => {
10+
if (state === undefined) return 5
11+
return action
12+
})
13+
14+
const nextStore = store.replaceReducer(nextReducer)
15+
16+
expect(nextStore).toBe(store)
17+
})
18+
})

0 commit comments

Comments
 (0)