Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have a strong opinion, but would it make sense to move this into tsconfig.test.json instead?

Copy link
Contributor Author

@OliverJAsh OliverJAsh Jan 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I considered that but then I couldn't think of a reason for it not to live here. It's probably better here because it increases strictness for the whole repo.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might cause issues with the type tests at some point.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't now, though 🤷

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it's probably okay for now but it might cause a hiccup or 2 at some point.

Reference

"target": "ESnext",
"types": ["vitest/globals", "vitest/importMeta"]
},
Expand Down
Loading