Skip to content

Commit

Permalink
fix CaseReducer to infer from argument, not return value
Browse files Browse the repository at this point in the history
  • Loading branch information
phryneas committed Jan 2, 2023
1 parent 6a7879f commit 8beef87
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/toolkit/src/createReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export type ActionMatcherDescriptionCollection<S> = Array<
export type CaseReducer<S = any, A extends Action = AnyAction> = (
state: Draft<S>,
action: A
) => S | void | Draft<S>
) => NoInfer<S> | void | Draft<NoInfer<S>>

/**
* A mapping from action types to case reducers for `createReducer()`.
Expand Down
15 changes: 8 additions & 7 deletions packages/toolkit/src/tests/createSlice.typetest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,22 @@ const value = actionCreators.anyKey
increment: (state: number, action) => state + action.payload,
decrement: (state: number, action) => state - action.payload,
},
extraReducers: {
[firstAction.type]: (state: number, action) =>
state + action.payload.count,
extraReducers: (builder) => {
builder.addCase(
firstAction,
(state, action) => state + action.payload.count
)
},
})

/* Reducer */

const reducer: Reducer<number, PayloadAction> = slice.reducer
expectType<Reducer<number, PayloadAction>>(slice.reducer)

// @ts-expect-error
const stringReducer: Reducer<string, PayloadAction> = slice.reducer
expectType<Reducer<string, PayloadAction>>(slice.reducer)
// @ts-expect-error
const anyActionReducer: Reducer<string, AnyAction> = slice.reducer

expectType<Reducer<string, AnyAction>>(slice.reducer)
/* Actions */

slice.actions.increment(1)
Expand Down

0 comments on commit 8beef87

Please sign in to comment.