Skip to content

Commit

Permalink
Return Reducer<S, AnyAction> from createReducer
Browse files Browse the repository at this point in the history
This is more correct as the generated reducer does, in fact,
support passing actions other than the ones explicitly handled.
Also, this fixes an incompatibility with `configureStore` (#102).
  • Loading branch information
denisw committed Jan 27, 2019
1 parent 0118f1f commit 164b973
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/createReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ export interface CaseReducersMapObject<S = any, A extends Action = AnyAction> {
export function createReducer<S = any, A extends Action = AnyAction>(
initialState: S,
actionsMap: CaseReducersMapObject<S, A>
): Reducer<S, A> {
): Reducer<S> {
return function(state = initialState, action): S {
// @ts-ignore createNextState() produces an Immutable<Draft<S>> rather
// than an Immutable<S>, and TypeScript cannot find out how to reconcile
// these two types.
return createNextState(state, (draft: Draft<S>) => {
const caseReducer = actionsMap[action.type]
return caseReducer ? caseReducer(draft, action) : undefined
return caseReducer ? caseReducer(draft, action as A) : undefined
})
}
}
7 changes: 2 additions & 5 deletions type-tests/files/createReducer.typetest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,10 @@ import { AnyAction, createReducer, Reducer } from 'redux-starter-kit'
decrement: decrementHandler
})

const numberReducer: Reducer<number, CounterAction> = reducer
const numberReducer: Reducer<number> = reducer

// typings:expect-error
const stringReducer: Reducer<string, CounterAction> = reducer

// typings:expect-error
const anyActionReducer: Reducer<number, AnyAction> = reducer
const stringReducer: Reducer<string> = reducer
}

/**
Expand Down

0 comments on commit 164b973

Please sign in to comment.