@@ -1245,7 +1245,13 @@ It should be an object with some of the following properties:
1245
1245
1246
1246
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.
1247
1247
1248
- The ` ReducerNamesOfType ` utility is exported to easily filter down to reducers that would be passed to the ` handle ` callback.
1248
+ 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.
1249
+
1250
+ ` ` ` ts no - transpile
1251
+ {
1252
+ [ReducerName in keyof CaseReducers ]: CaseReducers [ReducerName ] extends ReducerDefinition < typeof creatorType > ? ActionTypeHere : never
1253
+ }
1254
+ ` ` `
1249
1255
1250
1256
For example, with (a simplified version of) the ` asyncThunk ` creator:
1251
1257
@@ -1265,10 +1271,7 @@ declare module '@reduxjs/toolkit' {
1265
1271
{
1266
1272
// highlight-start
1267
1273
actions : {
1268
- [ReducerName in ReducerNamesOfType <
1269
- CaseReducers ,
1270
- typeof asyncThunkCreatorType
1271
- > ]: CaseReducers [ReducerName ] extends AsyncThunkReducerDefinition <
1274
+ [ReducerName in keyof CaseReducers ]: CaseReducers [ReducerName ] extends AsyncThunkReducerDefinition <
1272
1275
State ,
1273
1276
infer ThunkArg ,
1274
1277
infer Returned
@@ -1305,23 +1308,25 @@ declare module '@reduxjs/toolkit' {
1305
1308
) => PreparedCaseReducerDefinition < State , Prepare > ,
1306
1309
{
1307
1310
actions : {
1308
- [ReducerName in ReducerNamesOfType <
1309
- CaseReducers ,
1311
+ [ReducerName in keyof CaseReducers ]: CaseReducers [ReducerName ] extends ReducerDefinition <
1310
1312
typeof preparedReducerType
1311
- > ]: CaseReducers [ReducerName ] extends { prepare : any }
1312
- ? ActionCreatorForCaseReducerWithPrepare <
1313
- CaseReducers [ReducerName ],
1314
- SliceActionType < Name , ReducerName >
1315
- >
1313
+ >
1314
+ ? CaseReducers [ReducerName ] extends { prepare: any }
1315
+ ? ActionCreatorForCaseReducerWithPrepare <
1316
+ CaseReducers [ReducerName ],
1317
+ SliceActionType < Name , ReducerName >
1318
+ >
1319
+ : never
1316
1320
: never
1317
1321
}
1318
1322
// highlight-start
1319
1323
caseReducers : {
1320
- [ReducerName in ReducerNamesOfType <
1321
- CaseReducers ,
1324
+ [ReducerName in keyof CaseReducers ]: CaseReducers [ReducerName ] extends ReducerDefinition <
1322
1325
typeof preparedReducerType
1323
- > ]: CaseReducers [ReducerName ] extends { reducer : infer Reducer }
1324
- ? Reducer
1326
+ >
1327
+ ? CaseReducers [ReducerName ] extends { reducer: infer Reducer }
1328
+ ? Reducer
1329
+ : never
1325
1330
: never
1326
1331
}
1327
1332
// highlight-end
@@ -1375,6 +1380,10 @@ interface ToastReducerConfig<State> {
1375
1380
hidden?: CaseReducer < State , PayloadAction<undefined , string , { id : string }>>
1376
1381
}
1377
1382
1383
+ interface ToastReducerDefinition < State >
1384
+ extends ReducerDefinition < typeof toastCreatorType > ,
1385
+ ToastReducerConfig < State > {}
1386
+
1378
1387
interface ToastThunkCreator <
1379
1388
SliceName extends string ,
1380
1389
ReducerName extends string ,
@@ -1403,20 +1412,17 @@ declare module '@reduxjs/toolkit' {
1403
1412
Name extends string ,
1404
1413
> {
1405
1414
[toastCreatorType ]: ReducerCreatorEntry <
1406
- (
1407
- config : ToastReducerConfig < State > ,
1408
- ) => ToastReducerConfig < State > &
1409
- ReducerDefinition < typeof toastCreatorType > ,
1415
+ (config : ToastReducerConfig <State >) => ToastReducerDefinition < State > ,
1410
1416
{
1411
1417
actions : {
1412
- [ReducerName in ReducerNamesOfType <
1413
- typeof toastCreatorType
1414
- > ]: ToastThunkCreator < Name , ReducerName >
1418
+ [ReducerName in keyof CaseReducers ]: CaseReducers [ ReducerName ] extends ToastReducerDefinition < State >
1419
+ ? ToastThunkCreator < Name , ReducerName >
1420
+ : never
1415
1421
}
1416
1422
caseReducers : {
1417
- [ReducerName in ReducerNamesOfType <
1418
- typeof toastCreatorType
1419
- > ]: Required < ToastReducerConfig < State >>
1423
+ [ReducerName in keyof CaseReducers ]: CaseReducers [ ReducerName ] extends ToastReducerDefinition < State >
1424
+ ? Required < ToastReducerConfig < State >>
1425
+ : never
1420
1426
}
1421
1427
}
1422
1428
>
@@ -1598,38 +1604,41 @@ interface HistoryState<T> {
1598
1604
declare module ' @reduxjs/toolkit' {
1599
1605
export interface SliceReducerCreators<
1600
1606
State ,
1601
- CaseReducers extends
1602
- CreatorCaseReducers<State>,
1607
+ CaseReducers extends CreatorCaseReducers<State>,
1603
1608
Name extends string ,
1604
1609
> {
1605
- [paginationCreatorType ]: ReducerCreatorEntry <
1610
+ [historyCreatorType ]: ReducerCreatorEntry <
1606
1611
// make sure the creator is only called when state is compatibleState extends HistoryState<unknown>
1607
- ?
1608
- (this : ReducerCreators <State >) => {
1612
+ State extends HistoryState < any >
1613
+ ? (this : ReducerCreators <State >) => {
1609
1614
undo : CaseReducerDefinition < State , PayloadAction >
1610
1615
redo : CaseReducerDefinition < State , PayloadAction >
1611
- reset : ReducerDefinition < typeof paginationCreatorType > & {
1616
+ reset : ReducerDefinition < typeof historyCreatorType > & {
1612
1617
type : ' reset'
1613
1618
}
1614
1619
}
1615
- : never , {
1616
- actions: {
1617
- [ReducerName in ReducerNamesOfType <
1618
- CaseReducers ,
1619
- typeof historyMethodsCreatorType
1620
- > ]: CaseReducers [ReducerName ] extends { type: ' reset' }
1621
- ? PayloadActionCreator < void , SliceActionType < Name , ReducerName >>
1622
- : never
1623
- }
1624
- caseReducers : {
1625
- [ReducerName in ReducerNamesOfType <
1626
- CaseReducers ,
1627
- typeof historyMethodsCreatorType
1628
- > ]: CaseReducers [ReducerName ] extends { type: ' reset' }
1629
- ? CaseReducer < State , PayloadAction >
1630
- : never
1620
+ : never ,
1621
+ {
1622
+ actions : {
1623
+ [ReducerName in keyof CaseReducers ]: CaseReducers [ReducerName ] extends ReducerDefinition <
1624
+ typeof historyCreatorType
1625
+ >
1626
+ ? CaseReducers [ReducerName ] extends { type: ' reset' }
1627
+ ? PayloadActionCreator < void , SliceActionType < Name , ReducerName >>
1628
+ : never
1629
+ : never
1630
+ }
1631
+ caseReducers : {
1632
+ [ReducerName in keyof CaseReducers ]: CaseReducers [ReducerName ] extends ReducerDefinition <
1633
+ typeof historyCreatorType
1634
+ >
1635
+ ? CaseReducers [ReducerName ] extends { type: ' reset' }
1636
+ ? CaseReducer < State , PayloadAction >
1637
+ : never
1638
+ : never
1639
+ }
1631
1640
}
1632
- } >
1641
+ >
1633
1642
}
1634
1643
}
1635
1644
0 commit comments