Skip to content

Commit 28a200b

Browse files
committed
Merge branch 'create-slice-creators' into entity-methods-creator
2 parents c5f65f5 + bd73745 commit 28a200b

File tree

3 files changed

+58
-59
lines changed

3 files changed

+58
-59
lines changed

docs/api/createSlice.mdx

Lines changed: 58 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,7 +1245,13 @@ It should be an object with some of the following properties:
12451245
12461246
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.
12471247
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+
```
12491255
12501256
For example, with (a simplified version of) the `asyncThunk` creator:
12511257
@@ -1265,10 +1271,7 @@ declare module '@reduxjs/toolkit' {
12651271
{
12661272
// highlight-start
12671273
actions: {
1268-
[ReducerName in ReducerNamesOfType<
1269-
CaseReducers,
1270-
typeof asyncThunkCreatorType
1271-
>]: CaseReducers[ReducerName] extends AsyncThunkReducerDefinition<
1274+
[ReducerName in keyof CaseReducers]: CaseReducers[ReducerName] extends AsyncThunkReducerDefinition<
12721275
State,
12731276
infer ThunkArg,
12741277
infer Returned
@@ -1305,23 +1308,25 @@ declare module '@reduxjs/toolkit' {
13051308
) => PreparedCaseReducerDefinition<State, Prepare>,
13061309
{
13071310
actions: {
1308-
[ReducerName in ReducerNamesOfType<
1309-
CaseReducers,
1311+
[ReducerName in keyof CaseReducers]: CaseReducers[ReducerName] extends ReducerDefinition<
13101312
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
13161320
: never
13171321
}
13181322
// highlight-start
13191323
caseReducers: {
1320-
[ReducerName in ReducerNamesOfType<
1321-
CaseReducers,
1324+
[ReducerName in keyof CaseReducers]: CaseReducers[ReducerName] extends ReducerDefinition<
13221325
typeof preparedReducerType
1323-
>]: CaseReducers[ReducerName] extends { reducer: infer Reducer }
1324-
? Reducer
1326+
>
1327+
? CaseReducers[ReducerName] extends { reducer: infer Reducer }
1328+
? Reducer
1329+
: never
13251330
: never
13261331
}
13271332
// highlight-end
@@ -1375,6 +1380,10 @@ interface ToastReducerConfig<State> {
13751380
hidden?: CaseReducer<State, PayloadAction<undefined, string, { id: string }>>
13761381
}
13771382

1383+
interface ToastReducerDefinition<State>
1384+
extends ReducerDefinition<typeof toastCreatorType>,
1385+
ToastReducerConfig<State> {}
1386+
13781387
interface ToastThunkCreator<
13791388
SliceName extends string,
13801389
ReducerName extends string,
@@ -1403,20 +1412,17 @@ declare module '@reduxjs/toolkit' {
14031412
Name extends string,
14041413
> {
14051414
[toastCreatorType]: ReducerCreatorEntry<
1406-
(
1407-
config: ToastReducerConfig<State>,
1408-
) => ToastReducerConfig<State> &
1409-
ReducerDefinition<typeof toastCreatorType>,
1415+
(config: ToastReducerConfig<State>) => ToastReducerDefinition<State>,
14101416
{
14111417
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
14151421
}
14161422
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
14201426
}
14211427
}
14221428
>
@@ -1598,38 +1604,41 @@ interface HistoryState<T> {
15981604
declare module '@reduxjs/toolkit' {
15991605
export interface SliceReducerCreators<
16001606
State,
1601-
CaseReducers extends
1602-
CreatorCaseReducers<State>,
1607+
CaseReducers extends CreatorCaseReducers<State>,
16031608
Name extends string,
16041609
> {
1605-
[paginationCreatorType]: ReducerCreatorEntry<
1610+
[historyCreatorType]: ReducerCreatorEntry<
16061611
// 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>) => {
16091614
undo: CaseReducerDefinition<State, PayloadAction>
16101615
redo: CaseReducerDefinition<State, PayloadAction>
1611-
reset: ReducerDefinition<typeof paginationCreatorType> & {
1616+
reset: ReducerDefinition<typeof historyCreatorType> & {
16121617
type: 'reset'
16131618
}
16141619
}
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+
}
16311640
}
1632-
}>
1641+
>
16331642
}
16341643
}
16351644

packages/toolkit/src/createSlice.ts

Lines changed: 0 additions & 9 deletions
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

Lines changed: 0 additions & 1 deletion
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)