Skip to content

Commit 40af7fb

Browse files
committed
Merge branch 'create-slice-creators' into entity-methods-creator
2 parents e9d7fc8 + 99468f6 commit 40af7fb

File tree

4 files changed

+30
-111
lines changed

4 files changed

+30
-111
lines changed

docs/usage/custom-slice-creators.mdx

Lines changed: 10 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -685,11 +685,7 @@ Creators are registered by using module augmentation to add a new key (their uni
685685
const reducerCreatorType = Symbol('reducerCreatorType')
686686

687687
declare module '@reduxjs/toolkit' {
688-
export interface SliceReducerCreators<
689-
State,
690-
SliceName extends string,
691-
ReducerPath extends string,
692-
> {
688+
export interface SliceReducerCreators<State> {
693689
[reducerCreatorType]: () => ReducerDefinition<typeof reducerCreatorType>
694690
}
695691
export interface SliceReducerCreatorsExposes<
@@ -718,8 +714,6 @@ declare module '@reduxjs/toolkit' {
718714
The type parameters for `SliceReducerCreators` are:
719715

720716
- `State` - The state type used by the slice.
721-
- `SliceName` - The [`name`](../api/createSlice#name) used by the slice.
722-
- `ReducerPath` - The [`reducerPath`](../api/createSlice#reducerpath) used by the slice.
723717

724718
This is where you register the type of the `create` value of the creator definition (typically a function signature).
725719

@@ -733,11 +727,7 @@ However, this should be specifically included in the function signature, so Type
733727
const batchedCreatorType = Symbol('batchedCreatorType')
734728

735729
declare module '@reduxjs/toolkit' {
736-
export interface SliceReducerCreators<
737-
State,
738-
SliceName extends string,
739-
ReducerPath extends string,
740-
> {
730+
export interface SliceReducerCreators<State> {
741731
[batchedCreatorType]: <Payload>(
742732
// highlight-next-line
743733
this: ReducerCreators<State, {}>,
@@ -772,11 +762,7 @@ import {
772762
const batchedCreatorType = Symbol('batchedCreatorType')
773763

774764
declare module '@reduxjs/toolkit' {
775-
export interface SliceReducerCreators<
776-
State,
777-
SliceName extends string,
778-
ReducerPath extends string,
779-
> {
765+
export interface SliceReducerCreators<State> {
780766
[batchedCreatorType]: <Payload>(
781767
reducer: CaseReducer<State, PayloadAction<Payload>>,
782768
) => PreparedCaseReducerDefinition<
@@ -807,11 +793,7 @@ Sometimes it's useful to have a reducer creator that only works with a specific
807793
const loaderCreatorType = Symbol('loaderCreatorType')
808794

809795
declare module '@reduxjs/toolkit' {
810-
export interface SliceReducerCreators<
811-
State,
812-
SliceName extends string,
813-
ReducerPath extends string,
814-
> {
796+
export interface SliceReducerCreators<State> {
815797
// highlight-next-line
816798
[loaderCreatorType]: State extends { loading: boolean }
817799
? () => {
@@ -831,11 +813,7 @@ An alternative would be just using that required type _as_ the `State` type for
831813
const loaderCreatorType = Symbol('loaderCreatorType')
832814

833815
declare module '@reduxjs/toolkit' {
834-
export interface SliceReducerCreators<
835-
State,
836-
SliceName extends string,
837-
ReducerPath extends string,
838-
> {
816+
export interface SliceReducerCreators<State> {
839817
[loaderCreatorType]: () => {
840818
start: CaseReducerDefinition<{ loading: boolean }, PayloadAction>
841819
end: CaseReducerDefinition<{ loading: boolean }, PayloadAction>
@@ -878,11 +856,7 @@ For example, with (a simplified version of) the `asyncThunk` creator:
878856
const asyncThunkCreatorType = Symbol('asyncThunkCreatorType')
879857

880858
declare module '@reduxjs/toolkit' {
881-
export interface SliceReducerCreators<
882-
State,
883-
SliceName extends string,
884-
ReducerPath extends string,
885-
> {
859+
export interface SliceReducerCreators<State> {
886860
[asyncThunkCreatorType]: <ThunkArg, Returned>(
887861
payloadCreator: AsyncThunkPayloadCreator<ThunkArg, Returned>,
888862
) => AsyncThunkReducerDefinition<State, ThunkArg, Returned>
@@ -921,11 +895,7 @@ For example, with the `preparedReducer` creator:
921895
const preparedReducerType = Symbol('preparedReducerType')
922896

923897
declare module '@reduxjs/toolkit' {
924-
export interface SliceReducerCreators<
925-
State,
926-
SliceName extends string,
927-
ReducerPath extends string,
928-
> {
898+
export interface SliceReducerCreators<State> {
929899
[preparedReducerType]: <Prepare extends PrepareAction<any>>(
930900
prepare: Prepare,
931901
caseReducer: CaseReducer<State, ActionForPrepare<Prepare>>,
@@ -1023,11 +993,7 @@ interface ToastThunkCreator<
1023993

1024994
// register the creator types
1025995
declare module '@reduxjs/toolkit' {
1026-
export interface SliceReducerCreators<
1027-
State,
1028-
SliceName extends string,
1029-
ReducerPath extends string,
1030-
> {
996+
export interface SliceReducerCreators<State> {
1031997
[toastCreatorType]: (
1032998
config: ToastReducerConfig<State>,
1033999
) => ToastReducerDefinition<State>
@@ -1151,11 +1117,7 @@ interface PaginationState {
11511117
}
11521118

11531119
declare module '@reduxjs/toolkit' {
1154-
export interface SliceReducerCreators<
1155-
State,
1156-
SliceName extends string,
1157-
ReducerPath extends string,
1158-
> {
1120+
export interface SliceReducerCreators<State> {
11591121
// make sure the creator is only called when state is compatible
11601122
[paginationCreatorType]: State extends PaginationState
11611123
? (this: ReducerCreators<State>) => {
@@ -1221,11 +1183,7 @@ interface HistoryState<T> {
12211183
}
12221184

12231185
declare module '@reduxjs/toolkit' {
1224-
export interface SliceReducerCreators<
1225-
State,
1226-
SliceName extends string,
1227-
ReducerPath extends string,
1228-
> {
1186+
export interface SliceReducerCreators<State> {
12291187
// make sure the creator is only called when state is compatible
12301188
[historyCreatorType]: State extends HistoryState<any>
12311189
? (this: ReducerCreators<State>) => {

packages/toolkit/src/createSlice.ts

Lines changed: 18 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export enum ReducerType {
3939
entityMethods = 'entityMethods',
4040
}
4141

42-
export type RegisteredReducerType = keyof SliceReducerCreators<any, any, any>
42+
export type RegisteredReducerType = keyof SliceReducerCreators<any>
4343

4444
export type ReducerDefinition<
4545
T extends RegisteredReducerType = RegisteredReducerType,
@@ -51,11 +51,7 @@ export type CreatorCaseReducers<State> =
5151
| Record<string, ReducerDefinition>
5252
| SliceCaseReducers<State>
5353

54-
export interface SliceReducerCreators<
55-
State,
56-
SliceName extends string,
57-
ReducerPath extends string,
58-
> {
54+
export interface SliceReducerCreators<State> {
5955
[ReducerType.reducer]: {
6056
(
6157
caseReducer: CaseReducer<State, PayloadAction>,
@@ -113,28 +109,14 @@ export interface SliceReducerCreatorsExposes<
113109

114110
export type ReducerCreators<
115111
State,
116-
Name extends string = string,
117-
ReducerPath extends string = Name,
118112
CreatorMap extends Record<string, RegisteredReducerType> = {},
119113
> = {
120-
reducer: SliceReducerCreators<State, Name, ReducerPath>[ReducerType.reducer]
121-
preparedReducer: SliceReducerCreators<
122-
State,
123-
Name,
124-
ReducerPath
125-
>[ReducerType.reducerWithPrepare]
114+
reducer: SliceReducerCreators<State>[ReducerType.reducer]
115+
preparedReducer: SliceReducerCreators<State>[ReducerType.reducerWithPrepare]
126116
} & {
127-
[CreatorName in keyof CreatorMap as SliceReducerCreators<
128-
State,
129-
Name,
130-
ReducerPath
131-
>[CreatorMap[CreatorName]] extends never
117+
[CreatorName in keyof CreatorMap as SliceReducerCreators<State>[CreatorMap[CreatorName]] extends never
132118
? never
133-
: CreatorName]: SliceReducerCreators<
134-
State,
135-
Name,
136-
ReducerPath
137-
>[CreatorMap[CreatorName]]
119+
: CreatorName]: SliceReducerCreators<State>[CreatorMap[CreatorName]]
138120
}
139121

140122
interface InternalReducerHandlingContext<State> {
@@ -261,14 +243,14 @@ type DefinitionFromValue<
261243

262244
type ReducerDefinitionsForType<Type extends RegisteredReducerType> = {
263245
[CreatorType in RegisteredReducerType]: DefinitionFromValue<
264-
SliceReducerCreators<any, any, any>[CreatorType],
246+
SliceReducerCreators<any>[CreatorType],
265247
Type
266248
>
267249
}[RegisteredReducerType]
268250

269251
export type ReducerCreator<Type extends RegisteredReducerType> = {
270252
type: Type
271-
create: SliceReducerCreators<any, any, any>[Type]
253+
create: SliceReducerCreators<any>[Type]
272254
} & (ReducerDefinitionsForType<Type> extends never
273255
? {
274256
handle?<State>(
@@ -427,23 +409,16 @@ type InjectedSlice<
427409

428410
type CreatorCallback<
429411
State,
430-
Name extends string,
431-
ReducerPath extends string,
432412
CreatorMap extends Record<string, RegisteredReducerType>,
433413
> = (
434-
create: ReducerCreators<State, Name, ReducerPath, CreatorMap>,
414+
create: ReducerCreators<State, CreatorMap>,
435415
) => Record<string, ReducerDefinition>
436416

437417
type GetCaseReducers<
438418
State,
439-
Name extends string,
440-
ReducerPath extends string,
441419
CreatorMap extends Record<string, RegisteredReducerType>,
442-
CR extends SliceCaseReducers<State> | CreatorCallback<State, any, any, any>,
443-
> =
444-
CR extends CreatorCallback<State, Name, ReducerPath, CreatorMap>
445-
? ReturnType<CR>
446-
: CR
420+
CR extends SliceCaseReducers<State> | CreatorCallback<State, any>,
421+
> = CR extends CreatorCallback<State, CreatorMap> ? ReturnType<CR> : CR
447422

448423
/**
449424
* Options for `createSlice()`.
@@ -454,12 +429,7 @@ export interface CreateSliceOptions<
454429
State = any,
455430
CR extends
456431
| SliceCaseReducers<State>
457-
| CreatorCallback<
458-
State,
459-
Name,
460-
ReducerPath,
461-
CreatorMap
462-
> = SliceCaseReducers<State>,
432+
| CreatorCallback<State, CreatorMap> = SliceCaseReducers<State>,
463433
Name extends string = string,
464434
ReducerPath extends string = Name,
465435
Selectors extends SliceSelectors<State> = SliceSelectors<State>,
@@ -714,7 +684,7 @@ type SliceDefinedSelectors<
714684
*/
715685
export type ValidateSliceCaseReducers<
716686
S,
717-
ACR extends SliceCaseReducers<S> | CreatorCallback<S, any, any, any>,
687+
ACR extends SliceCaseReducers<S> | CreatorCallback<S, any>,
718688
> = ACR & {
719689
[T in keyof ACR]: ACR[T] extends {
720690
reducer(s: S, action?: infer A): any
@@ -773,8 +743,7 @@ export const preparedReducerCreator: ReducerCreator<ReducerType.reducerWithPrepa
773743

774744
const isCreatorCallback = (
775745
reducers: unknown,
776-
): reducers is CreatorCallback<any, any, any, any> =>
777-
typeof reducers === 'function'
746+
): reducers is CreatorCallback<any, any> => typeof reducers === 'function'
778747

779748
interface BuildCreateSliceConfig<
780749
CreatorMap extends Record<string, RegisteredReducerType>,
@@ -840,7 +809,7 @@ export function buildCreateSlice<
840809
State,
841810
CaseReducers extends
842811
| SliceCaseReducers<State>
843-
| CreatorCallback<State, Name, ReducerPath, CreatorMap>,
812+
| CreatorCallback<State, CreatorMap>,
844813
Name extends string,
845814
Selectors extends SliceSelectors<State>,
846815
ReducerPath extends string = Name,
@@ -855,7 +824,7 @@ export function buildCreateSlice<
855824
>,
856825
): Slice<
857826
State,
858-
GetCaseReducers<State, Name, ReducerPath, CreatorMap, CaseReducers>,
827+
GetCaseReducers<State, CreatorMap, CaseReducers>,
859828
Name,
860829
ReducerPath,
861830
Selectors
@@ -1058,7 +1027,7 @@ export function buildCreateSlice<
10581027
): Pick<
10591028
Slice<
10601029
State,
1061-
GetCaseReducers<State, Name, ReducerPath, CreatorMap, CaseReducers>,
1030+
GetCaseReducers<State, CreatorMap, CaseReducers>,
10621031
Name,
10631032
CurrentReducerPath,
10641033
Selectors
@@ -1124,7 +1093,7 @@ export function buildCreateSlice<
11241093

11251094
const slice: Slice<
11261095
State,
1127-
GetCaseReducers<State, Name, ReducerPath, CreatorMap, CaseReducers>,
1096+
GetCaseReducers<State, CreatorMap, CaseReducers>,
11281097
Name,
11291098
ReducerPath,
11301099
Selectors

packages/toolkit/src/tests/createSlice.test-d.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -884,11 +884,7 @@ interface AddToastThunk<Name extends string, ReducerName extends PropertyKey> {
884884
}
885885

886886
declare module '@reduxjs/toolkit' {
887-
export interface SliceReducerCreators<
888-
State,
889-
SliceName extends string,
890-
ReducerPath extends string,
891-
> {
887+
export interface SliceReducerCreators<State> {
892888
[toasterCreatorType]: State extends ToastState
893889
? () => ReducerDefinition<typeof toasterCreatorType>
894890
: never

packages/toolkit/src/tests/createSlice.test.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,11 +1195,7 @@ type PatchThunk<
11951195
}
11961196

11971197
declare module '@reduxjs/toolkit' {
1198-
export interface SliceReducerCreators<
1199-
State,
1200-
SliceName extends string,
1201-
ReducerPath extends string,
1202-
> {
1198+
export interface SliceReducerCreators<State> {
12031199
[loaderCreatorType]: (
12041200
reducers: Pick<LoaderReducerDefinition<State>, 'ended' | 'started'>,
12051201
) => LoaderReducerDefinition<State>

0 commit comments

Comments
 (0)