From c25ad5f0b4cfc01edb95fd5360711d8d84c83a0a Mon Sep 17 00:00:00 2001 From: EskiMojo14 Date: Sun, 11 Feb 2024 19:42:07 +0000 Subject: [PATCH 1/4] allow initialising combined slice reducer with no static slices --- packages/toolkit/src/combineSlices.ts | 14 +++++++------- packages/toolkit/src/tests/combineSlices.test-d.ts | 12 +++++++++--- packages/toolkit/src/tests/combineSlices.test.ts | 10 ++++++++++ 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/packages/toolkit/src/combineSlices.ts b/packages/toolkit/src/combineSlices.ts index 1229084799..59166ee5ba 100644 --- a/packages/toolkit/src/combineSlices.ts +++ b/packages/toolkit/src/combineSlices.ts @@ -359,15 +359,15 @@ const original = (state: any) => { return state[ORIGINAL_STATE] } -export function combineSlices< - Slices extends [ - AnySliceLike | ReducerMap, - ...Array, - ], ->(...slices: Slices): CombinedSliceReducer>> { +const noopReducer: Reducer> = (state = {}) => state + +export function combineSlices>( + ...slices: Slices +): CombinedSliceReducer>> { const reducerMap = Object.fromEntries(getReducers(slices)) - const getReducer = () => combineReducers(reducerMap) + const getReducer = () => + Object.keys(reducerMap).length ? combineReducers(reducerMap) : noopReducer let reducer = getReducer() diff --git a/packages/toolkit/src/tests/combineSlices.test-d.ts b/packages/toolkit/src/tests/combineSlices.test-d.ts index 5a7927fc84..759801b068 100644 --- a/packages/toolkit/src/tests/combineSlices.test-d.ts +++ b/packages/toolkit/src/tests/combineSlices.test-d.ts @@ -33,6 +33,12 @@ describe('type tests', () => { }>() }) + test('combineSlices allows for no initial reducers', () => { + const rootReducer = combineSlices() + + expectTypeOf(rootReducer(undefined, { type: '' })).toEqualTypeOf<{}>() + }) + test('withLazyLoadedSlices adds partial to state', () => { const rootReducer = combineSlices(stringSlice).withLazyLoadedSlices< WithSlice & WithSlice @@ -199,8 +205,8 @@ describe('type tests', () => { number: number }>() - expectTypeOf(withNumber(undefined, { type: '' }).number).toMatchTypeOf< - number - >() + expectTypeOf( + withNumber(undefined, { type: '' }).number, + ).toMatchTypeOf() }) }) diff --git a/packages/toolkit/src/tests/combineSlices.test.ts b/packages/toolkit/src/tests/combineSlices.test.ts index a74b675c68..8c263a94f5 100644 --- a/packages/toolkit/src/tests/combineSlices.test.ts +++ b/packages/toolkit/src/tests/combineSlices.test.ts @@ -64,6 +64,16 @@ describe('combineSlices', () => { api: api.reducer.getInitialState(), }) }) + it('allows passing no initial reducers', () => { + const combinedReducer = combineSlices() + + const result = combinedReducer(undefined, dummyAction()) + + expect(result).toEqual({}) + + // no-op if we have no reducers yet + expect(combinedReducer(result, dummyAction())).toBe(result) + }) describe('injects', () => { beforeEach(() => { vi.stubEnv('NODE_ENV', 'development') From 8e00d85895dcf25ad18ee734e13f02c0ba245799 Mon Sep 17 00:00:00 2001 From: EskiMojo14 Date: Sun, 11 Feb 2024 19:44:01 +0000 Subject: [PATCH 2/4] include withLazyLoadedSlices test --- packages/toolkit/src/tests/combineSlices.test-d.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/packages/toolkit/src/tests/combineSlices.test-d.ts b/packages/toolkit/src/tests/combineSlices.test-d.ts index 759801b068..5e34cd9cf5 100644 --- a/packages/toolkit/src/tests/combineSlices.test-d.ts +++ b/packages/toolkit/src/tests/combineSlices.test-d.ts @@ -37,6 +37,13 @@ describe('type tests', () => { const rootReducer = combineSlices() expectTypeOf(rootReducer(undefined, { type: '' })).toEqualTypeOf<{}>() + + const declaredLazy = + combineSlices().withLazyLoadedSlices>() + + expectTypeOf(declaredLazy(undefined, { type: '' })).toEqualTypeOf<{ + number?: number + }>() }) test('withLazyLoadedSlices adds partial to state', () => { From 02b338fbf4291459526193c9c3e4c3cb4311cf0b Mon Sep 17 00:00:00 2001 From: EskiMojo14 Date: Sun, 11 Feb 2024 19:46:11 +0000 Subject: [PATCH 3/4] remove warning from docs --- docs/api/combineSlices.mdx | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/docs/api/combineSlices.mdx b/docs/api/combineSlices.mdx index 81f8cb168a..ef8d475321 100644 --- a/docs/api/combineSlices.mdx +++ b/docs/api/combineSlices.mdx @@ -86,17 +86,6 @@ However, typing will not be able to account for this. It's best to ensure that a ::: -:::warning - -Like [`combineReducers`](https://redux.js.org/api/combinereducers), `combineSlices` requires at least one reducer at initialisation. - -```ts no-transpile -// will throw an error -const rootReducer = combineSlices() -``` - -::: - ## Return Value `combineSlices` returns a reducer function, with attached methods. From a548b41fc1968982d42419faefe5def478550a45 Mon Sep 17 00:00:00 2001 From: Ben Durrant Date: Sun, 11 Feb 2024 20:24:14 +0000 Subject: [PATCH 4/4] reword test --- packages/toolkit/src/tests/combineSlices.test-d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/toolkit/src/tests/combineSlices.test-d.ts b/packages/toolkit/src/tests/combineSlices.test-d.ts index 5e34cd9cf5..e06fe53c56 100644 --- a/packages/toolkit/src/tests/combineSlices.test-d.ts +++ b/packages/toolkit/src/tests/combineSlices.test-d.ts @@ -33,7 +33,7 @@ describe('type tests', () => { }>() }) - test('combineSlices allows for no initial reducers', () => { + test('combineSlices allows passing no initial reducers', () => { const rootReducer = combineSlices() expectTypeOf(rootReducer(undefined, { type: '' })).toEqualTypeOf<{}>()