From 8beef87572e6955a2b42ac0d582cb623041a8b4a Mon Sep 17 00:00:00 2001 From: Lenz Weber Date: Mon, 2 Jan 2023 23:23:44 +0100 Subject: [PATCH] fix CaseReducer to infer from argument, not return value --- packages/toolkit/src/createReducer.ts | 2 +- .../toolkit/src/tests/createSlice.typetest.ts | 15 ++++++++------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/packages/toolkit/src/createReducer.ts b/packages/toolkit/src/createReducer.ts index debd4d2381..2ccb730864 100644 --- a/packages/toolkit/src/createReducer.ts +++ b/packages/toolkit/src/createReducer.ts @@ -55,7 +55,7 @@ export type ActionMatcherDescriptionCollection = Array< export type CaseReducer = ( state: Draft, action: A -) => S | void | Draft +) => NoInfer | void | Draft> /** * A mapping from action types to case reducers for `createReducer()`. diff --git a/packages/toolkit/src/tests/createSlice.typetest.ts b/packages/toolkit/src/tests/createSlice.typetest.ts index 287187c32a..64c33078c5 100644 --- a/packages/toolkit/src/tests/createSlice.typetest.ts +++ b/packages/toolkit/src/tests/createSlice.typetest.ts @@ -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 = slice.reducer + expectType>(slice.reducer) // @ts-expect-error - const stringReducer: Reducer = slice.reducer + expectType>(slice.reducer) // @ts-expect-error - const anyActionReducer: Reducer = slice.reducer - + expectType>(slice.reducer) /* Actions */ slice.actions.increment(1)