Skip to content

Commit c7c88f2

Browse files
committed
Merge branch 'entity-methods-creator' into per-slice-creator
2 parents 7d18b19 + 40af7fb commit c7c88f2

File tree

4 files changed

+29
-125
lines changed

4 files changed

+29
-125
lines changed

docs/usage/custom-slice-creators.mdx

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

693693
declare module '@reduxjs/toolkit' {
694-
export interface SliceReducerCreators<
695-
State,
696-
SliceName extends string,
697-
ReducerPath extends string,
698-
> {
694+
export interface SliceReducerCreators<State> {
699695
[reducerCreatorType]: () => ReducerDefinition<typeof reducerCreatorType>
700696
}
701697
export interface SliceReducerCreatorsExposes<
@@ -724,8 +720,6 @@ declare module '@reduxjs/toolkit' {
724720
The type parameters for `SliceReducerCreators` are:
725721

726722
- `State` - The state type used by the slice.
727-
- `SliceName` - The [`name`](../api/createSlice#name) used by the slice.
728-
- `ReducerPath` - The [`reducerPath`](../api/createSlice#reducerpath) used by the slice.
729723

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

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

741735
declare module '@reduxjs/toolkit' {
742-
export interface SliceReducerCreators<
743-
State,
744-
SliceName extends string,
745-
ReducerPath extends string,
746-
> {
736+
export interface SliceReducerCreators<State> {
747737
[batchedCreatorType]: <Payload>(
748738
// highlight-next-line
749739
this: ReducerCreators<State, {}>,
@@ -778,11 +768,7 @@ import {
778768
const batchedCreatorType = Symbol('batchedCreatorType')
779769

780770
declare module '@reduxjs/toolkit' {
781-
export interface SliceReducerCreators<
782-
State,
783-
SliceName extends string,
784-
ReducerPath extends string,
785-
> {
771+
export interface SliceReducerCreators<State> {
786772
[batchedCreatorType]: <Payload>(
787773
reducer: CaseReducer<State, PayloadAction<Payload>>,
788774
) => PreparedCaseReducerDefinition<
@@ -813,11 +799,7 @@ Sometimes it's useful to have a reducer creator that only works with a specific
813799
const loaderCreatorType = Symbol('loaderCreatorType')
814800

815801
declare module '@reduxjs/toolkit' {
816-
export interface SliceReducerCreators<
817-
State,
818-
SliceName extends string,
819-
ReducerPath extends string,
820-
> {
802+
export interface SliceReducerCreators<State> {
821803
// highlight-next-line
822804
[loaderCreatorType]: State extends { loading: boolean }
823805
? () => {
@@ -837,11 +819,7 @@ An alternative would be just using that required type _as_ the `State` type for
837819
const loaderCreatorType = Symbol('loaderCreatorType')
838820

839821
declare module '@reduxjs/toolkit' {
840-
export interface SliceReducerCreators<
841-
State,
842-
SliceName extends string,
843-
ReducerPath extends string,
844-
> {
822+
export interface SliceReducerCreators<State> {
845823
[loaderCreatorType]: () => {
846824
start: CaseReducerDefinition<{ loading: boolean }, PayloadAction>
847825
end: CaseReducerDefinition<{ loading: boolean }, PayloadAction>
@@ -884,11 +862,7 @@ For example, with (a simplified version of) the `asyncThunk` creator:
884862
const asyncThunkCreatorType = Symbol('asyncThunkCreatorType')
885863

886864
declare module '@reduxjs/toolkit' {
887-
export interface SliceReducerCreators<
888-
State,
889-
SliceName extends string,
890-
ReducerPath extends string,
891-
> {
865+
export interface SliceReducerCreators<State> {
892866
[asyncThunkCreatorType]: <ThunkArg, Returned>(
893867
payloadCreator: AsyncThunkPayloadCreator<ThunkArg, Returned>,
894868
) => AsyncThunkReducerDefinition<State, ThunkArg, Returned>
@@ -927,11 +901,7 @@ For example, with the `preparedReducer` creator:
927901
const preparedReducerType = Symbol('preparedReducerType')
928902

929903
declare module '@reduxjs/toolkit' {
930-
export interface SliceReducerCreators<
931-
State,
932-
SliceName extends string,
933-
ReducerPath extends string,
934-
> {
904+
export interface SliceReducerCreators<State> {
935905
[preparedReducerType]: <Prepare extends PrepareAction<any>>(
936906
prepare: Prepare,
937907
caseReducer: CaseReducer<State, ActionForPrepare<Prepare>>,
@@ -1029,11 +999,7 @@ interface ToastThunkCreator<
1029999

10301000
// register the creator types
10311001
declare module '@reduxjs/toolkit' {
1032-
export interface SliceReducerCreators<
1033-
State,
1034-
SliceName extends string,
1035-
ReducerPath extends string,
1036-
> {
1002+
export interface SliceReducerCreators<State> {
10371003
[toastCreatorType]: (
10381004
config: ToastReducerConfig<State>,
10391005
) => ToastReducerDefinition<State>
@@ -1157,11 +1123,7 @@ interface PaginationState {
11571123
}
11581124

11591125
declare module '@reduxjs/toolkit' {
1160-
export interface SliceReducerCreators<
1161-
State,
1162-
SliceName extends string,
1163-
ReducerPath extends string,
1164-
> {
1126+
export interface SliceReducerCreators<State> {
11651127
// make sure the creator is only called when state is compatible
11661128
[paginationCreatorType]: State extends PaginationState
11671129
? (this: ReducerCreators<State>) => {
@@ -1227,11 +1189,7 @@ interface HistoryState<T> {
12271189
}
12281190

12291191
declare module '@reduxjs/toolkit' {
1230-
export interface SliceReducerCreators<
1231-
State,
1232-
SliceName extends string,
1233-
ReducerPath extends string,
1234-
> {
1192+
export interface SliceReducerCreators<State> {
12351193
// make sure the creator is only called when state is compatible
12361194
[historyCreatorType]: State extends HistoryState<any>
12371195
? (this: ReducerCreators<State>) => {

packages/toolkit/src/createSlice.ts

Lines changed: 17 additions & 63 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> {
@@ -269,14 +251,14 @@ type DefinitionFromValue<
269251

270252
type ReducerDefinitionsForType<Type extends RegisteredReducerType> = {
271253
[CreatorType in RegisteredReducerType]: DefinitionFromValue<
272-
SliceReducerCreators<any, any, any>[CreatorType],
254+
SliceReducerCreators<any>[CreatorType],
273255
Type
274256
>
275257
}[RegisteredReducerType]
276258

277259
export type ReducerCreator<Type extends RegisteredReducerType> = {
278260
type: Type
279-
create: SliceReducerCreators<any, any, any>[Type]
261+
create: SliceReducerCreators<any>[Type]
280262
} & (ReducerDefinitionsForType<Type> extends never
281263
? {
282264
handle?<State>(
@@ -435,23 +417,16 @@ type InjectedSlice<
435417

436418
type CreatorCallback<
437419
State,
438-
Name extends string,
439-
ReducerPath extends string,
440420
CreatorMap extends Record<string, RegisteredReducerType>,
441421
> = (
442-
create: ReducerCreators<State, Name, ReducerPath, CreatorMap>,
422+
create: ReducerCreators<State, CreatorMap>,
443423
) => Record<string, ReducerDefinition>
444424

445425
type GetCaseReducers<
446426
State,
447-
Name extends string,
448-
ReducerPath extends string,
449427
CreatorMap extends Record<string, RegisteredReducerType>,
450-
CR extends SliceCaseReducers<State> | CreatorCallback<State, any, any, any>,
451-
> =
452-
CR extends CreatorCallback<State, Name, ReducerPath, CreatorMap>
453-
? ReturnType<CR>
454-
: CR
428+
CR extends SliceCaseReducers<State> | CreatorCallback<State, any>,
429+
> = CR extends CreatorCallback<State, CreatorMap> ? ReturnType<CR> : CR
455430

456431
/**
457432
* Options for `createSlice()`.
@@ -464,8 +439,6 @@ export interface CreateSliceOptions<
464439
| SliceCaseReducers<State>
465440
| CreatorCallback<
466441
State,
467-
Name,
468-
ReducerPath,
469442
CreatorMap & SliceCreatorMap
470443
> = SliceCaseReducers<State>,
471444
Name extends string = string,
@@ -727,7 +700,7 @@ type SliceDefinedSelectors<
727700
*/
728701
export type ValidateSliceCaseReducers<
729702
S,
730-
ACR extends SliceCaseReducers<S> | CreatorCallback<S, any, any, any>,
703+
ACR extends SliceCaseReducers<S> | CreatorCallback<S, any>,
731704
> = ACR & {
732705
[T in keyof ACR]: ACR[T] extends {
733706
reducer(s: S, action?: infer A): any
@@ -786,8 +759,7 @@ export const preparedReducerCreator: ReducerCreator<ReducerType.reducerWithPrepa
786759

787760
const isCreatorCallback = (
788761
reducers: unknown,
789-
): reducers is CreatorCallback<any, any, any, any> =>
790-
typeof reducers === 'function'
762+
): reducers is CreatorCallback<any, any> => typeof reducers === 'function'
791763

792764
type CreatorOption<CreatorMap extends Record<string, RegisteredReducerType>> = {
793765
[Name in keyof CreatorMap]: Name extends 'reducer' | 'preparedReducer'
@@ -857,7 +829,7 @@ export function buildCreateSlice<
857829
State,
858830
CaseReducers extends
859831
| SliceCaseReducers<State>
860-
| CreatorCallback<State, Name, ReducerPath, CreatorMap & SliceCreatorMap>,
832+
| CreatorCallback<State, CreatorMap & SliceCreatorMap>,
861833
Name extends string,
862834
Selectors extends SliceSelectors<State>,
863835
ReducerPath extends string = Name,
@@ -874,13 +846,7 @@ export function buildCreateSlice<
874846
>,
875847
): Slice<
876848
State,
877-
GetCaseReducers<
878-
State,
879-
Name,
880-
ReducerPath,
881-
CreatorMap & SliceCreatorMap,
882-
CaseReducers
883-
>,
849+
GetCaseReducers<State, CreatorMap & SliceCreatorMap, CaseReducers>,
884850
Name,
885851
ReducerPath,
886852
Selectors
@@ -1104,13 +1070,7 @@ export function buildCreateSlice<
11041070
): Pick<
11051071
Slice<
11061072
State,
1107-
GetCaseReducers<
1108-
State,
1109-
Name,
1110-
ReducerPath,
1111-
CreatorMap & SliceCreatorMap,
1112-
CaseReducers
1113-
>,
1073+
GetCaseReducers<State, CreatorMap & SliceCreatorMap, CaseReducers>,
11141074
Name,
11151075
CurrentReducerPath,
11161076
Selectors
@@ -1176,13 +1136,7 @@ export function buildCreateSlice<
11761136

11771137
const slice: Slice<
11781138
State,
1179-
GetCaseReducers<
1180-
State,
1181-
Name,
1182-
ReducerPath,
1183-
CreatorMap & SliceCreatorMap,
1184-
CaseReducers
1185-
>,
1139+
GetCaseReducers<State, CreatorMap & SliceCreatorMap, CaseReducers>,
11861140
Name,
11871141
ReducerPath,
11881142
Selectors

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

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

917917
declare module '@reduxjs/toolkit' {
918-
export interface SliceReducerCreators<
919-
State,
920-
SliceName extends string,
921-
ReducerPath extends string,
922-
> {
918+
export interface SliceReducerCreators<State> {
923919
[toasterCreatorType]: State extends ToastState
924920
? () => ReducerDefinition<typeof toasterCreatorType>
925921
: never

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,11 +1233,7 @@ type PatchThunk<
12331233
}
12341234

12351235
declare module '@reduxjs/toolkit' {
1236-
export interface SliceReducerCreators<
1237-
State,
1238-
SliceName extends string,
1239-
ReducerPath extends string,
1240-
> {
1236+
export interface SliceReducerCreators<State> {
12411237
[loaderCreatorType]: (
12421238
reducers: Pick<LoaderReducerDefinition<State>, 'ended' | 'started'>,
12431239
) => LoaderReducerDefinition<State>

0 commit comments

Comments
 (0)