Skip to content

Commit

Permalink
fix(): ts typings, reducer now accepts void due to Immer usage
Browse files Browse the repository at this point in the history
  • Loading branch information
semoal committed Sep 20, 2021
1 parent d8044c4 commit a869a1f
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
1 change: 0 additions & 1 deletion examples/all-plugins-react-ts/src/models/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export const settings = createModel<RootModel>()({
state.isLightThemeOn = payload
? payload === 'light'
: !state.isLightThemeOn
return state
},
},
})
8 changes: 7 additions & 1 deletion packages/core/src/reduxStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,13 @@ export function createModelReducer<
action: Action
): TState => {
if (action.type in modelReducers) {
return modelReducers[action.type](state, action.payload, action.meta)
return modelReducers[action.type](
state,
action.payload,
action.meta
// we use augmentation because a reducer can return void due immer plugin,
// which makes optional returning the reducer state
) as TState
}

return state
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export type Reducer<TState = any> = (
state: TState,
payload?: Action['payload'],
meta?: Action['meta']
) => TState
) => TState | void

/** ************************** Model *************************** */

Expand Down Expand Up @@ -384,7 +384,7 @@ export type ExtractRematchDispatchersFromReducers<
export type ExtractRematchDispatcherFromReducer<TState, TReducer> =
TReducer extends () => any
? RematchDispatcher
: TReducer extends (state: TState, ...args: infer TRest) => TState
: TReducer extends (state: TState, ...args: infer TRest) => TState | void
? TRest extends []
? RematchDispatcher
: RematchDispatcher<TRest[0], TRest[1]>
Expand Down

0 comments on commit a869a1f

Please sign in to comment.