Skip to content

Commit 33015e5

Browse files
committed
use ReducerCreatorEntry type in docs
1 parent cd9f0f9 commit 33015e5

File tree

1 file changed

+92
-94
lines changed

1 file changed

+92
-94
lines changed

docs/api/createSlice.mdx

+92-94
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,7 @@ context
809809

810810
The Typescript system for custom slice creators uses a "creator registry" system similar to the module system for [RTK Query](/rtk-query/usage/customizing-create-api#creating-your-own-module).
811811

812-
Creators are registered by using module augmentation to add a new key (their unique `type`) to the `SliceReducerCreators` interface. The interface receives three type parameters (`State`, `CaseReducers` and `Name`), and each entry should have three keys (`create`, `actions`, and `caseReducers`).
812+
Creators are registered by using module augmentation to add a new key (their unique `type`) to the `SliceReducerCreators` interface. The interface receives three type parameters (`State`, `CaseReducers` and `Name`), and each entry should have use the `ReducerCreatorEntry` type utility.
813813

814814
```ts no-transpile
815815
const reducerCreatorType = Symbol()
@@ -820,22 +820,16 @@ declare module '@reduxjs/toolkit' {
820820
CaseReducers extends SliceCaseReducers<State> = SliceCaseReducers<State>,
821821
Name extends string = string
822822
> {
823-
[reducerCreatorType]: {
824-
create(): ReducerDefinition<typeof reducerCreatorType>
825-
actions: {}
826-
caseReducers: {}
827-
}
823+
[reducerCreatorType]: ReducerCreatorEntry<
824+
() => ReducerDefinition<typeof reducerCreatorType>
825+
>
828826
}
829827
}
830828
```
831829

832-
:::danger
833-
834-
All three keys must be provided, even if the creator only returns definitions for other creators. If the creator doesn't expose any actions/case reducers itself, leave those keys as `{}`.
830+
The `ReducerCreatorEntry<Create, Exposes>` utility has two type parameters:
835831

836-
:::
837-
838-
#### `create`
832+
#### `Create`
839833

840834
The signature of the `create` method of the creator definition.
841835

@@ -854,18 +848,16 @@ declare module '@reduxjs/toolkit' {
854848
CaseReducers extends SliceCaseReducers<State> = SliceCaseReducers<State>,
855849
Name extends string = string
856850
> {
857-
[batchedCreatorType]: {
858-
create<Payload>(
851+
[batchedCreatorType]: ReducerCreatorEntry<
852+
<Payload>(
859853
// highlight-next-line
860854
this: ReducerCreators<State, {}>,
861855
reducer: CaseReducer<State, PayloadAction<Payload>>
862-
): PreparedCaseReducerDefinition<
856+
) => PreparedCaseReducerDefinition<
863857
State,
864858
(payload: Payload) => { payload: Payload; meta: unknown }
865859
>
866-
actions: {}
867-
caseReducers: {}
868-
}
860+
>
869861
}
870862
}
871863

@@ -881,7 +873,13 @@ The second argument to the `ReducerCreators` type is a map from creator names to
881873

882874
:::
883875

884-
#### `actions`
876+
#### `Exposes`
877+
878+
The second type parameter for `ReducerCreatorEntry` is optional, but should be used if the creator will handle some reducer definitions itself. It indicates what actions and case reducers will be attached to the slice, and is used to determine the final types of `slice.actions` and `slice.caseReducers`.
879+
880+
It should be an object with some of the following properties:
881+
882+
##### `actions`
885883

886884
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.
887885

@@ -898,33 +896,32 @@ declare module '@reduxjs/toolkit' {
898896
CaseReducers extends SliceCaseReducers<State> = SliceCaseReducers<State>,
899897
Name extends string = string
900898
> {
901-
[asyncThunkCreatorType]: {
902-
create<ThunkArg, Returned>(
899+
[asyncThunkCreatorType]: ReducerCreatorEntry<
900+
<ThunkArg, Returned>(
903901
payloadCreator: AsyncThunkPayloadCreator<ThunkArg, Returned>
904-
): AsyncThunkReducerDefinition<State, ThunkArg, Returned>
905-
// highlight-start
906-
actions: {
907-
[ReducerName in ReducerNamesOfType<
908-
CaseReducers,
909-
typeof asyncThunkCreatorType
910-
>]: CaseReducers[ReducerName] extends AsyncThunkReducerDefinition<
911-
State,
912-
infer ThunkArg,
913-
infer Returned
914-
>
915-
? AsyncThunk<ThunkArg, Returned>
916-
: never
902+
) => AsyncThunkReducerDefinition<State, ThunkArg, Returned>,
903+
{
904+
// highlight-start
905+
actions: {
906+
[ReducerName in ReducerNamesOfType<
907+
CaseReducers,
908+
typeof asyncThunkCreatorType
909+
>]: CaseReducers[ReducerName] extends AsyncThunkReducerDefinition<
910+
State,
911+
infer ThunkArg,
912+
infer Returned
913+
>
914+
? AsyncThunk<ThunkArg, Returned>
915+
: never
916+
}
917+
// highlight-end
917918
}
918-
// highlight-end
919-
caseReducers: {}
920-
}
919+
>
921920
}
922921
}
923922
```
924923

925-
The `actions` returned from all the creators when passed the `CaseReducers` definitions are all intersected to create the final type for `slice.actions`.
926-
927-
#### `caseReducers`
924+
##### `caseReducers`
928925

929926
Similar to `actions`, except for `slice.caseReducers`.
930927

@@ -939,33 +936,35 @@ declare module '@reduxjs/toolkit' {
939936
CaseReducers extends SliceCaseReducers<State> = SliceCaseReducers<State>,
940937
Name extends string = string
941938
> {
942-
[preparedReducerType]: {
943-
create<Prepare extends PrepareAction<any>>(
939+
[preparedReducerType]: ReducerCreatorEntry<
940+
<Prepare extends PrepareAction<any>>(
944941
prepare: Prepare,
945942
caseReducer: CaseReducer<State, ActionForPrepare<Prepare>>
946-
): PreparedCaseReducerDefinition<State, Prepare>
947-
actions: {
948-
[ReducerName in ReducerNamesOfType<
949-
CaseReducers,
950-
typeof preparedReducerType
951-
>]: CaseReducers[ReducerName] extends { prepare: any }
952-
? ActionCreatorForCaseReducerWithPrepare<
953-
CaseReducers[ReducerName],
954-
SliceActionType<Name, ReducerName>
955-
>
956-
: never
957-
}
958-
// highlight-start
959-
caseReducers: {
960-
[ReducerName in ReducerNamesOfType<
961-
CaseReducers,
962-
typeof preparedReducerType
963-
>]: CaseReducers[ReducerName] extends { reducer: infer Reducer }
964-
? Reducer
965-
: never
943+
) => PreparedCaseReducerDefinition<State, Prepare>,
944+
{
945+
actions: {
946+
[ReducerName in ReducerNamesOfType<
947+
CaseReducers,
948+
typeof preparedReducerType
949+
>]: CaseReducers[ReducerName] extends { prepare: any }
950+
? ActionCreatorForCaseReducerWithPrepare<
951+
CaseReducers[ReducerName],
952+
SliceActionType<Name, ReducerName>
953+
>
954+
: never
955+
}
956+
// highlight-start
957+
caseReducers: {
958+
[ReducerName in ReducerNamesOfType<
959+
CaseReducers,
960+
typeof preparedReducerType
961+
>]: CaseReducers[ReducerName] extends { reducer: infer Reducer }
962+
? Reducer
963+
: never
964+
}
965+
// highlight-end
966966
}
967-
// highlight-end
968-
}
967+
>
969968
}
970969
}
971970
```
@@ -1022,21 +1021,24 @@ declare module '@reduxjs/toolkit' {
10221021
CaseReducers extends SliceCaseReducers<State> = SliceCaseReducers<State>,
10231022
Name extends string = string
10241023
> {
1025-
[toastCreatorType]: {
1026-
create(
1024+
[toastCreatorType]: ReducerCreatorEntry<
1025+
(
10271026
config: ToastReducerConfig<State>
1028-
): ToastReducerConfig<State> & ReducerDefinition<typeof toastCreatorType>
1029-
actions: {
1030-
[ReducerName in ReducerNamesOfType<
1031-
typeof toastCreatorType
1032-
>]: ToastThunkCreator<Name, ReducerName>
1033-
}
1034-
caseReducers: {
1035-
[ReducerName in ReducerNamesOfType<typeof toastCreatorType>]: Required<
1036-
ToastReducerConfig<State>
1037-
>
1027+
) => ToastReducerConfig<State> &
1028+
ReducerDefinition<typeof toastCreatorType>,
1029+
{
1030+
actions: {
1031+
[ReducerName in ReducerNamesOfType<
1032+
typeof toastCreatorType
1033+
>]: ToastThunkCreator<Name, ReducerName>
1034+
}
1035+
caseReducers: {
1036+
[ReducerName in ReducerNamesOfType<
1037+
typeof toastCreatorType
1038+
>]: Required<ToastReducerConfig<State>>
1039+
}
10381040
}
1039-
}
1041+
>
10401042
}
10411043
}
10421044

@@ -1148,18 +1150,16 @@ declare module '@reduxjs/toolkit' {
11481150
CaseReducers extends SliceCaseReducers<State> = SliceCaseReducers<State>,
11491151
Name extends string = string
11501152
> {
1151-
[paginationCreatorType]: {
1153+
[paginationCreatorType]: ReducerCreatorEntry<
11521154
// make sure the creator is only called when state is compatible
1153-
create(this: ReducerCreators<State>): State extends PaginationState
1154-
? {
1155+
State extends PaginationState
1156+
? (this: ReducerCreators<State>) => {
11551157
prevPage: CaseReducerDefinition<State, PayloadAction>
11561158
nextPage: CaseReducerDefinition<State, PayloadAction>
11571159
goToPage: CaseReducerDefinition<State, PayloadAction<number>>
11581160
}
11591161
: never
1160-
actions: {}
1161-
caseReducers: {}
1162-
}
1162+
>
11631163
}
11641164
}
11651165

@@ -1220,17 +1220,17 @@ declare module '@reduxjs/toolkit' {
12201220
CaseReducers extends SliceCaseReducers<State> = SliceCaseReducers<State>,
12211221
Name extends string = string
12221222
> {
1223-
[paginationCreatorType]: {
1224-
// make sure the creator is only called when state is compatible
1225-
create(this: ReducerCreators<State>): State extends HistoryState<unknown>
1226-
? {
1223+
[paginationCreatorType]: ReducerCreatorEntry<
1224+
// make sure the creator is only called when state is compatibleState extends HistoryState<unknown>
1225+
?
1226+
(this: ReducerCreators<State>) => {
12271227
undo: CaseReducerDefinition<State, PayloadAction>
12281228
redo: CaseReducerDefinition<State, PayloadAction>
12291229
reset: ReducerDefinition<typeof paginationCreatorType> & {
12301230
type: 'reset'
12311231
}
12321232
}
1233-
: never
1233+
: never, {
12341234
actions: {
12351235
[ReducerName in ReducerNamesOfType<
12361236
CaseReducers,
@@ -1247,7 +1247,7 @@ declare module '@reduxjs/toolkit' {
12471247
? CaseReducer<State, PayloadAction>
12481248
: never
12491249
}
1250-
}
1250+
}>
12511251
}
12521252
}
12531253

@@ -1329,8 +1329,8 @@ declare module '@reduxjs/toolkit' {
13291329
CaseReducers extends SliceCaseReducers<State> = SliceCaseReducers<State>,
13301330
Name extends string = string
13311331
> {
1332-
[undoableCreatorType]: {
1333-
create: State extends HistoryState<infer Data>
1332+
[undoableCreatorType]: ReducerCreatorEntry<
1333+
State extends HistoryState<infer Data>
13341334
? {
13351335
<A extends Action & { meta?: UndoableMeta }>(
13361336
reducer: CaseReducer<Data, A>
@@ -1345,9 +1345,7 @@ declare module '@reduxjs/toolkit' {
13451345
): { payload: Payload; meta: UndoableMeta | undefined }
13461346
}
13471347
: never
1348-
actions: {}
1349-
caseReducers: {}
1350-
}
1348+
>
13511349
}
13521350
}
13531351

0 commit comments

Comments
 (0)