diff --git a/src/types/reducers.ts b/src/types/reducers.ts index 146fc137f6..ca234898f2 100644 --- a/src/types/reducers.ts +++ b/src/types/reducers.ts @@ -106,7 +106,7 @@ export type PreloadedStateShapeFromReducersMapObject = M[keyof M] extends ? { [P in keyof M]: M[P] extends ( inputState: infer InputState, - action: UnknownAction + action: ActionFromReducersMapObject ) => any ? InputState : never diff --git a/test/typescript/reducers.test-d.ts b/test/typescript/reducers.test-d.ts index 46266aea92..26e0e17977 100644 --- a/test/typescript/reducers.test-d.ts +++ b/test/typescript/reducers.test-d.ts @@ -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', () => { @@ -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 + }> + }) + + test('`combineReducer` has correct return type when given a custom action', () => { + type MyAction = { type: 'foo' } + + type State = string + const nested: Reducer = (state = 'foo') => state + + type Combined = { nested: State } + + // Expect no error + const combined: Reducer< + Combined, + MyAction, + Partial + > = combineReducers({ nested }) + }) }) diff --git a/tsconfig.base.json b/tsconfig.base.json index 8f209a52e9..f53b7ca935 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -20,6 +20,7 @@ "resolveJsonModule": true, "skipLibCheck": true, "strict": true, + "exactOptionalPropertyTypes": true, "target": "ESnext", "types": ["vitest/globals", "vitest/importMeta"] },