Skip to content

Commit c25ad5f

Browse files
committed
allow initialising combined slice reducer with no static slices
1 parent fd24f6f commit c25ad5f

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

packages/toolkit/src/combineSlices.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -359,15 +359,15 @@ const original = (state: any) => {
359359
return state[ORIGINAL_STATE]
360360
}
361361

362-
export function combineSlices<
363-
Slices extends [
364-
AnySliceLike | ReducerMap,
365-
...Array<AnySliceLike | ReducerMap>,
366-
],
367-
>(...slices: Slices): CombinedSliceReducer<Id<InitialState<Slices>>> {
362+
const noopReducer: Reducer<Record<string, any>> = (state = {}) => state
363+
364+
export function combineSlices<Slices extends Array<AnySliceLike | ReducerMap>>(
365+
...slices: Slices
366+
): CombinedSliceReducer<Id<InitialState<Slices>>> {
368367
const reducerMap = Object.fromEntries<Reducer>(getReducers(slices))
369368

370-
const getReducer = () => combineReducers(reducerMap)
369+
const getReducer = () =>
370+
Object.keys(reducerMap).length ? combineReducers(reducerMap) : noopReducer
371371

372372
let reducer = getReducer()
373373

packages/toolkit/src/tests/combineSlices.test-d.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ describe('type tests', () => {
3333
}>()
3434
})
3535

36+
test('combineSlices allows for no initial reducers', () => {
37+
const rootReducer = combineSlices()
38+
39+
expectTypeOf(rootReducer(undefined, { type: '' })).toEqualTypeOf<{}>()
40+
})
41+
3642
test('withLazyLoadedSlices adds partial to state', () => {
3743
const rootReducer = combineSlices(stringSlice).withLazyLoadedSlices<
3844
WithSlice<typeof numberSlice> & WithSlice<typeof exampleApi>
@@ -199,8 +205,8 @@ describe('type tests', () => {
199205
number: number
200206
}>()
201207

202-
expectTypeOf(withNumber(undefined, { type: '' }).number).toMatchTypeOf<
203-
number
204-
>()
208+
expectTypeOf(
209+
withNumber(undefined, { type: '' }).number,
210+
).toMatchTypeOf<number>()
205211
})
206212
})

packages/toolkit/src/tests/combineSlices.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@ describe('combineSlices', () => {
6464
api: api.reducer.getInitialState(),
6565
})
6666
})
67+
it('allows passing no initial reducers', () => {
68+
const combinedReducer = combineSlices()
69+
70+
const result = combinedReducer(undefined, dummyAction())
71+
72+
expect(result).toEqual({})
73+
74+
// no-op if we have no reducers yet
75+
expect(combinedReducer(result, dummyAction())).toBe(result)
76+
})
6777
describe('injects', () => {
6878
beforeEach(() => {
6979
vi.stubEnv('NODE_ENV', 'development')

0 commit comments

Comments
 (0)