Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeScript: combineReducers returns reducer that accepts partial state #2809

Closed
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 index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export type ReducersMapObject<S = any, A extends Action = Action> = {
* object, and builds a state object with the same shape.
*/
export function combineReducers<S>(reducers: ReducersMapObject<S, any>): Reducer<S>;
export function combineReducers<S, A extends Action = AnyAction>(reducers: ReducersMapObject<S, A>): Reducer<S, A>;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It seems a shame to break apart from the Reducer type here, but without adding an extra generic to Reducer, I don't see a way around it?

export function combineReducers<S, A extends Action = AnyAction>(reducers: ReducersMapObject<S, A>): (state: Partial<S> | undefined, action: A) => S;


/* store */
Expand Down
7 changes: 5 additions & 2 deletions test/typescript/reducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,14 @@ function simple() {
// typings:expect-error
reducer('string', { type: 'INCREMENT' })

type RootState = { sub: State; sub2: State };

// Combined reducer also accepts any action.
const combined = combineReducers({ sub: reducer })
const combined = combineReducers({ sub: reducer, sub2: reducer })

let cs: { sub: State } = combined(undefined, { type: 'init' })
let cs: RootState = combined(undefined, { type: 'init' })
cs = combined(cs, { type: 'INCREMENT', count: 10 })
cs = combined({ sub: 1 }, { type: 'INCREMENT', count: 10 })

// Combined reducer's state is strictly checked.
// typings:expect-error
Expand Down