Skip to content

Commit fa9dc9d

Browse files
authored
Fix issue with combineReducer returning incorrect type when given a custom action (#4765)
1 parent d129739 commit fa9dc9d

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

src/types/reducers.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export type PreloadedStateShapeFromReducersMapObject<M> = M[keyof M] extends
106106
? {
107107
[P in keyof M]: M[P] extends (
108108
inputState: infer InputState,
109-
action: UnknownAction
109+
action: ActionFromReducersMapObject<M>
110110
) => any
111111
? InputState
112112
: never

test/typescript/reducers.test-d.ts

+33-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import type { Action, AnyAction, Reducer, ReducersMapObject } from 'redux'
1+
import type {
2+
Action,
3+
AnyAction,
4+
PreloadedStateShapeFromReducersMapObject,
5+
Reducer,
6+
ReducersMapObject
7+
} from 'redux'
28
import { combineReducers } from 'redux'
39

410
describe('type tests', () => {
@@ -265,4 +271,30 @@ describe('type tests', () => {
265271
>()
266272
}
267273
})
274+
275+
test('`PreloadedStateShapeFromReducersMapObject` has correct type when given a custom action', () => {
276+
type MyAction = { type: 'foo' }
277+
278+
// TODO: not sure how to write this test??
279+
// Expect this to match type `{ nested: string | undefined; }`
280+
type P = PreloadedStateShapeFromReducersMapObject<{
281+
nested: Reducer<string, MyAction>
282+
}>
283+
})
284+
285+
test('`combineReducer` has correct return type when given a custom action', () => {
286+
type MyAction = { type: 'foo' }
287+
288+
type State = string
289+
const nested: Reducer<State, MyAction> = (state = 'foo') => state
290+
291+
type Combined = { nested: State }
292+
293+
// Expect no error
294+
const combined: Reducer<
295+
Combined,
296+
MyAction,
297+
Partial<Combined>
298+
> = combineReducers({ nested })
299+
})
268300
})

tsconfig.base.json

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"resolveJsonModule": true,
2121
"skipLibCheck": true,
2222
"strict": true,
23+
"exactOptionalPropertyTypes": true,
2324
"target": "ESnext",
2425
"types": ["vitest/globals", "vitest/importMeta"]
2526
},

0 commit comments

Comments
 (0)