Skip to content

Commit

Permalink
Fix issue with combineReducer returning incorrect type when given a…
Browse files Browse the repository at this point in the history
… custom action (#4765)
  • Loading branch information
OliverJAsh authored Jan 23, 2025
1 parent d129739 commit fa9dc9d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/types/reducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export type PreloadedStateShapeFromReducersMapObject<M> = M[keyof M] extends
? {
[P in keyof M]: M[P] extends (
inputState: infer InputState,
action: UnknownAction
action: ActionFromReducersMapObject<M>
) => any
? InputState
: never
Expand Down
34 changes: 33 additions & 1 deletion test/typescript/reducers.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import type { Action, AnyAction, Reducer, ReducersMapObject } from 'redux'
import type {
Action,
AnyAction,
PreloadedStateShapeFromReducersMapObject,
Reducer,
ReducersMapObject
} from 'redux'
import { combineReducers } from 'redux'

describe('type tests', () => {
Expand Down Expand Up @@ -265,4 +271,30 @@ describe('type tests', () => {
>()
}
})

test('`PreloadedStateShapeFromReducersMapObject` has correct type when given a custom action', () => {
type MyAction = { type: 'foo' }

// TODO: not sure how to write this test??
// Expect this to match type `{ nested: string | undefined; }`
type P = PreloadedStateShapeFromReducersMapObject<{
nested: Reducer<string, MyAction>
}>
})

test('`combineReducer` has correct return type when given a custom action', () => {
type MyAction = { type: 'foo' }

type State = string
const nested: Reducer<State, MyAction> = (state = 'foo') => state

type Combined = { nested: State }

// Expect no error
const combined: Reducer<
Combined,
MyAction,
Partial<Combined>
> = combineReducers({ nested })
})
})
1 change: 1 addition & 0 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"resolveJsonModule": true,
"skipLibCheck": true,
"strict": true,
"exactOptionalPropertyTypes": true,
"target": "ESnext",
"types": ["vitest/globals", "vitest/importMeta"]
},
Expand Down

0 comments on commit fa9dc9d

Please sign in to comment.