@@ -1146,7 +1146,13 @@ It should be an object with some of the following properties:
1146
1146
1147
1147
The actions property will typically be a [mapped type](https://www.typescriptlang.org/docs/handbook/2/mapped-types.html) over the ` CaseReducers ` type parameter, returning what the creator's ` handle ` would expose when given that definition.
1148
1148
1149
- The ` ReducerNamesOfType ` utility is exported to easily filter down to reducers that would be passed to the ` handle ` callback.
1149
+ In order to ensure that the definitions are correctly filtered to only include those handled by the creator, a conditional type should be used, typically checking the definition extends a ` ReducerDefinition ` with the right type.
1150
+
1151
+ ` ` ` ts no - transpile
1152
+ {
1153
+ [ReducerName in keyof CaseReducers ]: CaseReducers [ReducerName ] extends ReducerDefinition < typeof creatorType > ? ActionTypeHere : never
1154
+ }
1155
+ ` ` `
1150
1156
1151
1157
For example, with (a simplified version of) the ` asyncThunk ` creator:
1152
1158
@@ -1166,10 +1172,7 @@ declare module '@reduxjs/toolkit' {
1166
1172
{
1167
1173
// highlight-start
1168
1174
actions : {
1169
- [ReducerName in ReducerNamesOfType <
1170
- CaseReducers ,
1171
- typeof asyncThunkCreatorType
1172
- > ]: CaseReducers [ReducerName ] extends AsyncThunkReducerDefinition <
1175
+ [ReducerName in keyof CaseReducers ]: CaseReducers [ReducerName ] extends AsyncThunkReducerDefinition <
1173
1176
State ,
1174
1177
infer ThunkArg ,
1175
1178
infer Returned
@@ -1206,23 +1209,25 @@ declare module '@reduxjs/toolkit' {
1206
1209
) => PreparedCaseReducerDefinition < State , Prepare > ,
1207
1210
{
1208
1211
actions : {
1209
- [ReducerName in ReducerNamesOfType <
1210
- CaseReducers ,
1212
+ [ReducerName in keyof CaseReducers ]: CaseReducers [ReducerName ] extends ReducerDefinition <
1211
1213
typeof preparedReducerType
1212
- > ]: CaseReducers [ReducerName ] extends { prepare : any }
1213
- ? ActionCreatorForCaseReducerWithPrepare <
1214
- CaseReducers [ReducerName ],
1215
- SliceActionType < Name , ReducerName >
1216
- >
1214
+ >
1215
+ ? CaseReducers [ReducerName ] extends { prepare: any }
1216
+ ? ActionCreatorForCaseReducerWithPrepare <
1217
+ CaseReducers [ReducerName ],
1218
+ SliceActionType < Name , ReducerName >
1219
+ >
1220
+ : never
1217
1221
: never
1218
1222
}
1219
1223
// highlight-start
1220
1224
caseReducers : {
1221
- [ReducerName in ReducerNamesOfType <
1222
- CaseReducers ,
1225
+ [ReducerName in keyof CaseReducers ]: CaseReducers [ReducerName ] extends ReducerDefinition <
1223
1226
typeof preparedReducerType
1224
- > ]: CaseReducers [ReducerName ] extends { reducer : infer Reducer }
1225
- ? Reducer
1227
+ >
1228
+ ? CaseReducers [ReducerName ] extends { reducer: infer Reducer }
1229
+ ? Reducer
1230
+ : never
1226
1231
: never
1227
1232
}
1228
1233
// highlight-end
@@ -1276,6 +1281,10 @@ interface ToastReducerConfig<State> {
1276
1281
hidden?: CaseReducer < State , PayloadAction<undefined , string , { id : string }>>
1277
1282
}
1278
1283
1284
+ interface ToastReducerDefinition < State >
1285
+ extends ReducerDefinition < typeof toastCreatorType > ,
1286
+ ToastReducerConfig < State > {}
1287
+
1279
1288
interface ToastThunkCreator <
1280
1289
SliceName extends string ,
1281
1290
ReducerName extends string ,
@@ -1304,20 +1313,17 @@ declare module '@reduxjs/toolkit' {
1304
1313
Name extends string ,
1305
1314
> {
1306
1315
[toastCreatorType ]: ReducerCreatorEntry <
1307
- (
1308
- config : ToastReducerConfig < State > ,
1309
- ) => ToastReducerConfig < State > &
1310
- ReducerDefinition < typeof toastCreatorType > ,
1316
+ (config : ToastReducerConfig <State >) => ToastReducerDefinition < State > ,
1311
1317
{
1312
1318
actions : {
1313
- [ReducerName in ReducerNamesOfType <
1314
- typeof toastCreatorType
1315
- > ]: ToastThunkCreator < Name , ReducerName >
1319
+ [ReducerName in keyof CaseReducers ]: CaseReducers [ ReducerName ] extends ToastReducerDefinition < State >
1320
+ ? ToastThunkCreator < Name , ReducerName >
1321
+ : never
1316
1322
}
1317
1323
caseReducers : {
1318
- [ReducerName in ReducerNamesOfType <
1319
- typeof toastCreatorType
1320
- > ]: Required < ToastReducerConfig < State >>
1324
+ [ReducerName in keyof CaseReducers ]: CaseReducers [ ReducerName ] extends ToastReducerDefinition < State >
1325
+ ? Required < ToastReducerConfig < State >>
1326
+ : never
1321
1327
}
1322
1328
}
1323
1329
>
@@ -1499,38 +1505,41 @@ interface HistoryState<T> {
1499
1505
declare module ' @reduxjs/toolkit' {
1500
1506
export interface SliceReducerCreators<
1501
1507
State ,
1502
- CaseReducers extends
1503
- CreatorCaseReducers<State>,
1508
+ CaseReducers extends CreatorCaseReducers<State>,
1504
1509
Name extends string ,
1505
1510
> {
1506
- [paginationCreatorType ]: ReducerCreatorEntry <
1511
+ [historyCreatorType ]: ReducerCreatorEntry <
1507
1512
// make sure the creator is only called when state is compatibleState extends HistoryState<unknown>
1508
- ?
1509
- (this : ReducerCreators <State >) => {
1513
+ State extends HistoryState < any >
1514
+ ? (this : ReducerCreators <State >) => {
1510
1515
undo : CaseReducerDefinition < State , PayloadAction >
1511
1516
redo : CaseReducerDefinition < State , PayloadAction >
1512
- reset : ReducerDefinition < typeof paginationCreatorType > & {
1517
+ reset : ReducerDefinition < typeof historyCreatorType > & {
1513
1518
type : ' reset'
1514
1519
}
1515
1520
}
1516
- : never , {
1517
- actions: {
1518
- [ReducerName in ReducerNamesOfType <
1519
- CaseReducers ,
1520
- typeof historyMethodsCreatorType
1521
- > ]: CaseReducers [ReducerName ] extends { type: ' reset' }
1522
- ? PayloadActionCreator < void , SliceActionType < Name , ReducerName >>
1523
- : never
1524
- }
1525
- caseReducers : {
1526
- [ReducerName in ReducerNamesOfType <
1527
- CaseReducers ,
1528
- typeof historyMethodsCreatorType
1529
- > ]: CaseReducers [ReducerName ] extends { type: ' reset' }
1530
- ? CaseReducer < State , PayloadAction >
1531
- : never
1521
+ : never ,
1522
+ {
1523
+ actions : {
1524
+ [ReducerName in keyof CaseReducers ]: CaseReducers [ReducerName ] extends ReducerDefinition <
1525
+ typeof historyCreatorType
1526
+ >
1527
+ ? CaseReducers [ReducerName ] extends { type: ' reset' }
1528
+ ? PayloadActionCreator < void , SliceActionType < Name , ReducerName >>
1529
+ : never
1530
+ : never
1531
+ }
1532
+ caseReducers : {
1533
+ [ReducerName in keyof CaseReducers ]: CaseReducers [ReducerName ] extends ReducerDefinition <
1534
+ typeof historyCreatorType
1535
+ >
1536
+ ? CaseReducers [ReducerName ] extends { type: ' reset' }
1537
+ ? CaseReducer < State , PayloadAction >
1538
+ : never
1539
+ : never
1540
+ }
1532
1541
}
1533
- } >
1542
+ >
1534
1543
}
1535
1544
}
1536
1545
0 commit comments