Skip to content

Commit bd73745

Browse files
committed
remove ReducerNamesOfType
1 parent ebfd549 commit bd73745

File tree

3 files changed

+58
-59
lines changed

3 files changed

+58
-59
lines changed

docs/api/createSlice.mdx

+58-49
Original file line numberDiff line numberDiff line change
@@ -1146,7 +1146,13 @@ It should be an object with some of the following properties:
11461146
11471147
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.
11481148
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+
```
11501156
11511157
For example, with (a simplified version of) the `asyncThunk` creator:
11521158
@@ -1166,10 +1172,7 @@ declare module '@reduxjs/toolkit' {
11661172
{
11671173
// highlight-start
11681174
actions: {
1169-
[ReducerName in ReducerNamesOfType<
1170-
CaseReducers,
1171-
typeof asyncThunkCreatorType
1172-
>]: CaseReducers[ReducerName] extends AsyncThunkReducerDefinition<
1175+
[ReducerName in keyof CaseReducers]: CaseReducers[ReducerName] extends AsyncThunkReducerDefinition<
11731176
State,
11741177
infer ThunkArg,
11751178
infer Returned
@@ -1206,23 +1209,25 @@ declare module '@reduxjs/toolkit' {
12061209
) => PreparedCaseReducerDefinition<State, Prepare>,
12071210
{
12081211
actions: {
1209-
[ReducerName in ReducerNamesOfType<
1210-
CaseReducers,
1212+
[ReducerName in keyof CaseReducers]: CaseReducers[ReducerName] extends ReducerDefinition<
12111213
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
12171221
: never
12181222
}
12191223
// highlight-start
12201224
caseReducers: {
1221-
[ReducerName in ReducerNamesOfType<
1222-
CaseReducers,
1225+
[ReducerName in keyof CaseReducers]: CaseReducers[ReducerName] extends ReducerDefinition<
12231226
typeof preparedReducerType
1224-
>]: CaseReducers[ReducerName] extends { reducer: infer Reducer }
1225-
? Reducer
1227+
>
1228+
? CaseReducers[ReducerName] extends { reducer: infer Reducer }
1229+
? Reducer
1230+
: never
12261231
: never
12271232
}
12281233
// highlight-end
@@ -1276,6 +1281,10 @@ interface ToastReducerConfig<State> {
12761281
hidden?: CaseReducer<State, PayloadAction<undefined, string, { id: string }>>
12771282
}
12781283

1284+
interface ToastReducerDefinition<State>
1285+
extends ReducerDefinition<typeof toastCreatorType>,
1286+
ToastReducerConfig<State> {}
1287+
12791288
interface ToastThunkCreator<
12801289
SliceName extends string,
12811290
ReducerName extends string,
@@ -1304,20 +1313,17 @@ declare module '@reduxjs/toolkit' {
13041313
Name extends string,
13051314
> {
13061315
[toastCreatorType]: ReducerCreatorEntry<
1307-
(
1308-
config: ToastReducerConfig<State>,
1309-
) => ToastReducerConfig<State> &
1310-
ReducerDefinition<typeof toastCreatorType>,
1316+
(config: ToastReducerConfig<State>) => ToastReducerDefinition<State>,
13111317
{
13121318
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
13161322
}
13171323
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
13211327
}
13221328
}
13231329
>
@@ -1499,38 +1505,41 @@ interface HistoryState<T> {
14991505
declare module '@reduxjs/toolkit' {
15001506
export interface SliceReducerCreators<
15011507
State,
1502-
CaseReducers extends
1503-
CreatorCaseReducers<State>,
1508+
CaseReducers extends CreatorCaseReducers<State>,
15041509
Name extends string,
15051510
> {
1506-
[paginationCreatorType]: ReducerCreatorEntry<
1511+
[historyCreatorType]: ReducerCreatorEntry<
15071512
// 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>) => {
15101515
undo: CaseReducerDefinition<State, PayloadAction>
15111516
redo: CaseReducerDefinition<State, PayloadAction>
1512-
reset: ReducerDefinition<typeof paginationCreatorType> & {
1517+
reset: ReducerDefinition<typeof historyCreatorType> & {
15131518
type: 'reset'
15141519
}
15151520
}
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+
}
15321541
}
1533-
}>
1542+
>
15341543
}
15351544
}
15361545

packages/toolkit/src/createSlice.ts

-9
Original file line numberDiff line numberDiff line change
@@ -321,15 +321,6 @@ export type ReducerCreator<Type extends RegisteredReducerType> = {
321321
): void
322322
})
323323

324-
export type ReducerNamesOfType<
325-
CaseReducers extends CreatorCaseReducers<any>,
326-
Type extends RegisteredReducerType,
327-
> = {
328-
[ReducerName in keyof CaseReducers]: CaseReducers[ReducerName] extends ReducerDefinition<Type>
329-
? ReducerName
330-
: never
331-
}[keyof CaseReducers]
332-
333324
interface InjectIntoConfig<NewReducerPath extends string> extends InjectConfig {
334325
reducerPath?: NewReducerPath
335326
}

packages/toolkit/src/index.ts

-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ export type {
8484
SliceSelectors,
8585
SliceReducerCreators,
8686
ReducerDefinition,
87-
ReducerNamesOfType,
8887
ReducerCreatorEntry,
8988
ReducerCreator,
9089
ReducerDetails,

0 commit comments

Comments
 (0)