Skip to content

Commit 99468f6

Browse files
committed
only pass State to SliceReducerCreators
1 parent 30f0429 commit 99468f6

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
@@ -586,11 +586,7 @@ Creators are registered by using module augmentation to add a new key (their uni
586586
const reducerCreatorType = Symbol('reducerCreatorType')
587587

588588
declare module '@reduxjs/toolkit' {
589-
export interface SliceReducerCreators<
590-
State,
591-
SliceName extends string,
592-
ReducerPath extends string,
593-
> {
589+
export interface SliceReducerCreators<State> {
594590
[reducerCreatorType]: () => ReducerDefinition<typeof reducerCreatorType>
595591
}
596592
export interface SliceReducerCreatorsExposes<
@@ -619,8 +615,6 @@ declare module '@reduxjs/toolkit' {
619615
The type parameters for `SliceReducerCreators` are:
620616

621617
- `State` - The state type used by the slice.
622-
- `SliceName` - The [`name`](../api/createSlice#name) used by the slice.
623-
- `ReducerPath` - The [`reducerPath`](../api/createSlice#reducerpath) used by the slice.
624618

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

@@ -634,11 +628,7 @@ However, this should be specifically included in the function signature, so Type
634628
const batchedCreatorType = Symbol('batchedCreatorType')
635629

636630
declare module '@reduxjs/toolkit' {
637-
export interface SliceReducerCreators<
638-
State,
639-
SliceName extends string,
640-
ReducerPath extends string,
641-
> {
631+
export interface SliceReducerCreators<State> {
642632
[batchedCreatorType]: <Payload>(
643633
// highlight-next-line
644634
this: ReducerCreators<State, {}>,
@@ -673,11 +663,7 @@ import {
673663
const batchedCreatorType = Symbol('batchedCreatorType')
674664

675665
declare module '@reduxjs/toolkit' {
676-
export interface SliceReducerCreators<
677-
State,
678-
SliceName extends string,
679-
ReducerPath extends string,
680-
> {
666+
export interface SliceReducerCreators<State> {
681667
[batchedCreatorType]: <Payload>(
682668
reducer: CaseReducer<State, PayloadAction<Payload>>,
683669
) => PreparedCaseReducerDefinition<
@@ -708,11 +694,7 @@ Sometimes it's useful to have a reducer creator that only works with a specific
708694
const loaderCreatorType = Symbol('loaderCreatorType')
709695

710696
declare module '@reduxjs/toolkit' {
711-
export interface SliceReducerCreators<
712-
State,
713-
SliceName extends string,
714-
ReducerPath extends string,
715-
> {
697+
export interface SliceReducerCreators<State> {
716698
// highlight-next-line
717699
[loaderCreatorType]: State extends { loading: boolean }
718700
? () => {
@@ -732,11 +714,7 @@ An alternative would be just using that required type _as_ the `State` type for
732714
const loaderCreatorType = Symbol('loaderCreatorType')
733715

734716
declare module '@reduxjs/toolkit' {
735-
export interface SliceReducerCreators<
736-
State,
737-
SliceName extends string,
738-
ReducerPath extends string,
739-
> {
717+
export interface SliceReducerCreators<State> {
740718
[loaderCreatorType]: () => {
741719
start: CaseReducerDefinition<{ loading: boolean }, PayloadAction>
742720
end: CaseReducerDefinition<{ loading: boolean }, PayloadAction>
@@ -779,11 +757,7 @@ For example, with (a simplified version of) the `asyncThunk` creator:
779757
const asyncThunkCreatorType = Symbol('asyncThunkCreatorType')
780758

781759
declare module '@reduxjs/toolkit' {
782-
export interface SliceReducerCreators<
783-
State,
784-
SliceName extends string,
785-
ReducerPath extends string,
786-
> {
760+
export interface SliceReducerCreators<State> {
787761
[asyncThunkCreatorType]: <ThunkArg, Returned>(
788762
payloadCreator: AsyncThunkPayloadCreator<ThunkArg, Returned>,
789763
) => AsyncThunkReducerDefinition<State, ThunkArg, Returned>
@@ -822,11 +796,7 @@ For example, with the `preparedReducer` creator:
822796
const preparedReducerType = Symbol('preparedReducerType')
823797

824798
declare module '@reduxjs/toolkit' {
825-
export interface SliceReducerCreators<
826-
State,
827-
SliceName extends string,
828-
ReducerPath extends string,
829-
> {
799+
export interface SliceReducerCreators<State> {
830800
[preparedReducerType]: <Prepare extends PrepareAction<any>>(
831801
prepare: Prepare,
832802
caseReducer: CaseReducer<State, ActionForPrepare<Prepare>>,
@@ -924,11 +894,7 @@ interface ToastThunkCreator<
924894

925895
// register the creator types
926896
declare module '@reduxjs/toolkit' {
927-
export interface SliceReducerCreators<
928-
State,
929-
SliceName extends string,
930-
ReducerPath extends string,
931-
> {
897+
export interface SliceReducerCreators<State> {
932898
[toastCreatorType]: (
933899
config: ToastReducerConfig<State>,
934900
) => ToastReducerDefinition<State>
@@ -1052,11 +1018,7 @@ interface PaginationState {
10521018
}
10531019

10541020
declare module '@reduxjs/toolkit' {
1055-
export interface SliceReducerCreators<
1056-
State,
1057-
SliceName extends string,
1058-
ReducerPath extends string,
1059-
> {
1021+
export interface SliceReducerCreators<State> {
10601022
// make sure the creator is only called when state is compatible
10611023
[paginationCreatorType]: State extends PaginationState
10621024
? (this: ReducerCreators<State>) => {
@@ -1122,11 +1084,7 @@ interface HistoryState<T> {
11221084
}
11231085

11241086
declare module '@reduxjs/toolkit' {
1125-
export interface SliceReducerCreators<
1126-
State,
1127-
SliceName extends string,
1128-
ReducerPath extends string,
1129-
> {
1087+
export interface SliceReducerCreators<State> {
11301088
// make sure the creator is only called when state is compatible
11311089
[historyCreatorType]: State extends HistoryState<any>
11321090
? (this: ReducerCreators<State>) => {

packages/toolkit/src/createSlice.ts

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

41-
export type RegisteredReducerType = keyof SliceReducerCreators<any, any, any>
41+
export type RegisteredReducerType = keyof SliceReducerCreators<any>
4242

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

53-
export interface SliceReducerCreators<
54-
State,
55-
SliceName extends string,
56-
ReducerPath extends string,
57-
> {
53+
export interface SliceReducerCreators<State> {
5854
[ReducerType.reducer]: {
5955
(
6056
caseReducer: CaseReducer<State, PayloadAction>,
@@ -111,28 +107,14 @@ export interface SliceReducerCreatorsExposes<
111107

112108
export type ReducerCreators<
113109
State,
114-
Name extends string = string,
115-
ReducerPath extends string = Name,
116110
CreatorMap extends Record<string, RegisteredReducerType> = {},
117111
> = {
118-
reducer: SliceReducerCreators<State, Name, ReducerPath>[ReducerType.reducer]
119-
preparedReducer: SliceReducerCreators<
120-
State,
121-
Name,
122-
ReducerPath
123-
>[ReducerType.reducerWithPrepare]
112+
reducer: SliceReducerCreators<State>[ReducerType.reducer]
113+
preparedReducer: SliceReducerCreators<State>[ReducerType.reducerWithPrepare]
124114
} & {
125-
[CreatorName in keyof CreatorMap as SliceReducerCreators<
126-
State,
127-
Name,
128-
ReducerPath
129-
>[CreatorMap[CreatorName]] extends never
115+
[CreatorName in keyof CreatorMap as SliceReducerCreators<State>[CreatorMap[CreatorName]] extends never
130116
? never
131-
: CreatorName]: SliceReducerCreators<
132-
State,
133-
Name,
134-
ReducerPath
135-
>[CreatorMap[CreatorName]]
117+
: CreatorName]: SliceReducerCreators<State>[CreatorMap[CreatorName]]
136118
}
137119

138120
interface InternalReducerHandlingContext<State> {
@@ -259,14 +241,14 @@ type DefinitionFromValue<
259241

260242
type ReducerDefinitionsForType<Type extends RegisteredReducerType> = {
261243
[CreatorType in RegisteredReducerType]: DefinitionFromValue<
262-
SliceReducerCreators<any, any, any>[CreatorType],
244+
SliceReducerCreators<any>[CreatorType],
263245
Type
264246
>
265247
}[RegisteredReducerType]
266248

267249
export type ReducerCreator<Type extends RegisteredReducerType> = {
268250
type: Type
269-
create: SliceReducerCreators<any, any, any>[Type]
251+
create: SliceReducerCreators<any>[Type]
270252
} & (ReducerDefinitionsForType<Type> extends never
271253
? {
272254
handle?<State>(
@@ -425,23 +407,16 @@ type InjectedSlice<
425407

426408
type CreatorCallback<
427409
State,
428-
Name extends string,
429-
ReducerPath extends string,
430410
CreatorMap extends Record<string, RegisteredReducerType>,
431411
> = (
432-
create: ReducerCreators<State, Name, ReducerPath, CreatorMap>,
412+
create: ReducerCreators<State, CreatorMap>,
433413
) => Record<string, ReducerDefinition>
434414

435415
type GetCaseReducers<
436416
State,
437-
Name extends string,
438-
ReducerPath extends string,
439417
CreatorMap extends Record<string, RegisteredReducerType>,
440-
CR extends SliceCaseReducers<State> | CreatorCallback<State, any, any, any>,
441-
> =
442-
CR extends CreatorCallback<State, Name, ReducerPath, CreatorMap>
443-
? ReturnType<CR>
444-
: CR
418+
CR extends SliceCaseReducers<State> | CreatorCallback<State, any>,
419+
> = CR extends CreatorCallback<State, CreatorMap> ? ReturnType<CR> : CR
445420

446421
/**
447422
* Options for `createSlice()`.
@@ -452,12 +427,7 @@ export interface CreateSliceOptions<
452427
State = any,
453428
CR extends
454429
| SliceCaseReducers<State>
455-
| CreatorCallback<
456-
State,
457-
Name,
458-
ReducerPath,
459-
CreatorMap
460-
> = SliceCaseReducers<State>,
430+
| CreatorCallback<State, CreatorMap> = SliceCaseReducers<State>,
461431
Name extends string = string,
462432
ReducerPath extends string = Name,
463433
Selectors extends SliceSelectors<State> = SliceSelectors<State>,
@@ -712,7 +682,7 @@ type SliceDefinedSelectors<
712682
*/
713683
export type ValidateSliceCaseReducers<
714684
S,
715-
ACR extends SliceCaseReducers<S> | CreatorCallback<S, any, any, any>,
685+
ACR extends SliceCaseReducers<S> | CreatorCallback<S, any>,
716686
> = ACR & {
717687
[T in keyof ACR]: ACR[T] extends {
718688
reducer(s: S, action?: infer A): any
@@ -771,8 +741,7 @@ export const preparedReducerCreator: ReducerCreator<ReducerType.reducerWithPrepa
771741

772742
const isCreatorCallback = (
773743
reducers: unknown,
774-
): reducers is CreatorCallback<any, any, any, any> =>
775-
typeof reducers === 'function'
744+
): reducers is CreatorCallback<any, any> => typeof reducers === 'function'
776745

777746
interface BuildCreateSliceConfig<
778747
CreatorMap extends Record<string, RegisteredReducerType>,
@@ -838,7 +807,7 @@ export function buildCreateSlice<
838807
State,
839808
CaseReducers extends
840809
| SliceCaseReducers<State>
841-
| CreatorCallback<State, Name, ReducerPath, CreatorMap>,
810+
| CreatorCallback<State, CreatorMap>,
842811
Name extends string,
843812
Selectors extends SliceSelectors<State>,
844813
ReducerPath extends string = Name,
@@ -853,7 +822,7 @@ export function buildCreateSlice<
853822
>,
854823
): Slice<
855824
State,
856-
GetCaseReducers<State, Name, ReducerPath, CreatorMap, CaseReducers>,
825+
GetCaseReducers<State, CreatorMap, CaseReducers>,
857826
Name,
858827
ReducerPath,
859828
Selectors
@@ -1056,7 +1025,7 @@ export function buildCreateSlice<
10561025
): Pick<
10571026
Slice<
10581027
State,
1059-
GetCaseReducers<State, Name, ReducerPath, CreatorMap, CaseReducers>,
1028+
GetCaseReducers<State, CreatorMap, CaseReducers>,
10601029
Name,
10611030
CurrentReducerPath,
10621031
Selectors
@@ -1122,7 +1091,7 @@ export function buildCreateSlice<
11221091

11231092
const slice: Slice<
11241093
State,
1125-
GetCaseReducers<State, Name, ReducerPath, CreatorMap, CaseReducers>,
1094+
GetCaseReducers<State, CreatorMap, CaseReducers>,
11261095
Name,
11271096
ReducerPath,
11281097
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)