Skip to content

Commit 97fa0bd

Browse files
committed
remove defaults from SliceReducerCreator
1 parent 8c1c6f0 commit 97fa0bd

File tree

5 files changed

+73
-67
lines changed

5 files changed

+73
-67
lines changed

docs/api/createSlice.mdx

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,10 +1115,9 @@ const reducerCreatorType = Symbol()
11151115

11161116
declare module '@reduxjs/toolkit' {
11171117
export interface SliceReducerCreators<
1118-
State = any,
1119-
CaseReducers extends
1120-
CreatorCaseReducers<State> = CreatorCaseReducers<State>,
1121-
Name extends string = string,
1118+
State,
1119+
CaseReducers extends CreatorCaseReducers<State>,
1120+
Name extends string,
11221121
> {
11231122
[reducerCreatorType]: ReducerCreatorEntry<
11241123
() => ReducerDefinition<typeof reducerCreatorType>
@@ -1144,10 +1143,9 @@ const batchedCreatorType = Symbol()
11441143

11451144
declare module '@reduxjs/toolkit' {
11461145
export interface SliceReducerCreators<
1147-
State = any,
1148-
CaseReducers extends
1149-
CreatorCaseReducers<State> = CreatorCaseReducers<State>,
1150-
Name extends string = string,
1146+
State,
1147+
CaseReducers extends CreatorCaseReducers<State>,
1148+
Name extends string,
11511149
> {
11521150
[batchedCreatorType]: ReducerCreatorEntry<
11531151
<Payload>(
@@ -1183,10 +1181,9 @@ const loaderCreatorType = Symbol()
11831181

11841182
declare module '@reduxjs/toolkit' {
11851183
export interface SliceReducerCreators<
1186-
State = any,
1187-
CaseReducers extends
1188-
CreatorCaseReducers<State> = CreatorCaseReducers<State>,
1189-
Name extends string = string,
1184+
State,
1185+
CaseReducers extends CreatorCaseReducers<State>,
1186+
Name extends string,
11901187
> {
11911188
[loaderCreatorType]: ReducerCreatorEntry<
11921189
// highlight-next-line
@@ -1210,10 +1207,9 @@ const loaderCreatorType = Symbol()
12101207

12111208
declare module '@reduxjs/toolkit' {
12121209
export interface SliceReducerCreators<
1213-
State = any,
1214-
CaseReducers extends
1215-
CreatorCaseReducers<State> = CreatorCaseReducers<State>,
1216-
Name extends string = string,
1210+
State,
1211+
CaseReducers extends CreatorCaseReducers<State>,
1212+
Name extends string,
12171213
> {
12181214
[loaderCreatorType]: ReducerCreatorEntry<
12191215
() => {
@@ -1246,10 +1242,9 @@ const asyncThunkCreatorType = Symbol()
12461242

12471243
declare module '@reduxjs/toolkit' {
12481244
export interface SliceReducerCreators<
1249-
State = any,
1250-
CaseReducers extends
1251-
CreatorCaseReducers<State> = CreatorCaseReducers<State>,
1252-
Name extends string = string,
1245+
State,
1246+
CaseReducers extends CreatorCaseReducers<State>,
1247+
Name extends string,
12531248
> {
12541249
[asyncThunkCreatorType]: ReducerCreatorEntry<
12551250
<ThunkArg, Returned>(
@@ -1287,10 +1282,9 @@ const preparedReducerType = Symbol()
12871282

12881283
declare module '@reduxjs/toolkit' {
12891284
export interface SliceReducerCreators<
1290-
State = any,
1291-
CaseReducers extends
1292-
CreatorCaseReducers<State> = CreatorCaseReducers<State>,
1293-
Name extends string = string,
1285+
State,
1286+
CaseReducers extends CreatorCaseReducers<State>,
1287+
Name extends string,
12941288
> {
12951289
[preparedReducerType]: ReducerCreatorEntry<
12961290
<Prepare extends PrepareAction<any>>(
@@ -1392,10 +1386,9 @@ interface ToastThunkCreator<
13921386
// register the creator types
13931387
declare module '@reduxjs/toolkit' {
13941388
export interface SliceReducerCreators<
1395-
State = any,
1396-
CaseReducers extends
1397-
CreatorCaseReducers<State> = CreatorCaseReducers<State>,
1398-
Name extends string = string,
1389+
State,
1390+
CaseReducers extends CreatorCaseReducers<State>,
1391+
Name extends string,
13991392
> {
14001393
[toastCreatorType]: ReducerCreatorEntry<
14011394
(
@@ -1522,10 +1515,9 @@ interface PaginationState {
15221515

15231516
declare module '@reduxjs/toolkit' {
15241517
export interface SliceReducerCreators<
1525-
State = any,
1526-
CaseReducers extends
1527-
CreatorCaseReducers<State> = CreatorCaseReducers<State>,
1528-
Name extends string = string,
1518+
State,
1519+
CaseReducers extends CreatorCaseReducers<State>,
1520+
Name extends string,
15291521
> {
15301522
[paginationCreatorType]: ReducerCreatorEntry<
15311523
// make sure the creator is only called when state is compatible
@@ -1593,9 +1585,10 @@ interface HistoryState<T> {
15931585

15941586
declare module '@reduxjs/toolkit' {
15951587
export interface SliceReducerCreators<
1596-
State = any,
1597-
CaseReducers extends CreatorCaseReducers<State> = CreatorCaseReducers<State>,
1598-
Name extends string = string
1588+
State,
1589+
CaseReducers extends
1590+
CreatorCaseReducers<State>,
1591+
Name extends string,
15991592
> {
16001593
[paginationCreatorType]: ReducerCreatorEntry<
16011594
// make sure the creator is only called when state is compatibleState extends HistoryState<unknown>
@@ -1702,10 +1695,9 @@ interface UndoableMeta {
17021695

17031696
declare module '@reduxjs/toolkit' {
17041697
export interface SliceReducerCreators<
1705-
State = any,
1706-
CaseReducers extends
1707-
CreatorCaseReducers<State> = CreatorCaseReducers<State>,
1708-
Name extends string = string,
1698+
State,
1699+
CaseReducers extends CreatorCaseReducers<State>,
1700+
Name extends string,
17091701
> {
17101702
[undoableCreatorType]: ReducerCreatorEntry<
17111703
State extends HistoryState<infer Data>

packages/toolkit/src/createSlice.ts

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

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

4444
export interface ReducerDefinition<
4545
T extends RegisteredReducerType = RegisteredReducerType,
@@ -68,9 +68,9 @@ export type CreatorCaseReducers<State> =
6868
| SliceCaseReducers<State>
6969

7070
export interface SliceReducerCreators<
71-
State = any,
72-
CaseReducers extends CreatorCaseReducers<State> = CreatorCaseReducers<State>,
73-
Name extends string = string,
71+
State,
72+
CaseReducers extends CreatorCaseReducers<State>,
73+
Name extends string,
7474
> {
7575
[ReducerType.reducer]: ReducerCreatorEntry<
7676
{
@@ -179,12 +179,20 @@ export type ReducerCreators<
179179
State,
180180
CreatorMap extends Record<string, RegisteredReducerType> = {},
181181
> = {
182-
reducer: SliceReducerCreators<State>[ReducerType.reducer]['create']
183-
preparedReducer: SliceReducerCreators<State>[ReducerType.reducerWithPrepare]['create']
182+
reducer: SliceReducerCreators<State, any, any>[ReducerType.reducer]['create']
183+
preparedReducer: SliceReducerCreators<
184+
State,
185+
any,
186+
any
187+
>[ReducerType.reducerWithPrepare]['create']
184188
} & {
185-
[Name in keyof CreatorMap as SliceReducerCreators<State>[CreatorMap[Name]]['create'] extends never
189+
[Name in keyof CreatorMap as SliceReducerCreators<
190+
State,
191+
any,
192+
any
193+
>[CreatorMap[Name]]['create'] extends never
186194
? never
187-
: Name]: SliceReducerCreators<State>[CreatorMap[Name]]['create']
195+
: Name]: SliceReducerCreators<State, any, any>[CreatorMap[Name]]['create']
188196
}
189197

190198
interface ReducerHandlingContext<State> {
@@ -289,23 +297,31 @@ type RecursiveExtractDefinition<
289297
: never)
290298

291299
type ReducerDefinitionsForType<Type extends RegisteredReducerType> = {
292-
[CreatorType in keyof SliceReducerCreators]:
300+
[CreatorType in RegisteredReducerType]:
293301
| RecursiveExtractDefinition<
294-
ReturnType<SliceReducerCreators[CreatorType]['create']>,
302+
ReturnType<SliceReducerCreators<any, any, any>[CreatorType]['create']>,
295303
Type
296304
>
297305
| {
298-
[K in keyof SliceReducerCreators[CreatorType]['create']]: SliceReducerCreators[CreatorType]['create'][K] extends (
306+
[K in keyof SliceReducerCreators<
307+
any,
308+
any,
309+
any
310+
>[CreatorType]['create']]: SliceReducerCreators<
311+
any,
312+
any,
313+
any
314+
>[CreatorType]['create'][K] extends (
299315
...args: any[]
300316
) => infer Definitions
301317
? RecursiveExtractDefinition<Definitions, Type>
302318
: never
303-
}[keyof SliceReducerCreators[CreatorType]['create']]
304-
}[keyof SliceReducerCreators]
319+
}[keyof SliceReducerCreators<any, any, any>[CreatorType]['create']]
320+
}[keyof SliceReducerCreators<any, any, any>]
305321

306322
export type ReducerCreator<Type extends RegisteredReducerType> = {
307323
type: Type
308-
create: SliceReducerCreators[Type]['create']
324+
create: SliceReducerCreators<any, any, any>[Type]['create']
309325
} & (ReducerDefinitionsForType<Type> extends never
310326
? {}
311327
: {
@@ -368,7 +384,7 @@ export interface Slice<
368384
* The individual case reducer functions that were passed in the `reducers` parameter.
369385
* This enables reuse and testing if they were defined inline when calling `createSlice`.
370386
*/
371-
caseReducers: SliceDefinedCaseReducers<CaseReducers>
387+
caseReducers: SliceDefinedCaseReducers<CaseReducers, Name>
372388

373389
/**
374390
* Provides access to the initial state value given to the slice.
@@ -778,12 +794,13 @@ type SliceDefinedCaseReducers<
778794
CaseReducers extends
779795
| SliceCaseReducers<any>
780796
| Record<string, ReducerDefinition>,
797+
SliceName extends string = string,
781798
> = Id<
782799
UnionToIntersection<
783800
SliceReducerCreators<
784801
any,
785802
CaseReducers,
786-
any
803+
SliceName
787804
>[RegisteredReducerType]['caseReducers']
788805
>
789806
>

packages/toolkit/src/entities/slice_creator.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,9 @@ type EntityMethodsCreator<State> =
9595

9696
declare module '@reduxjs/toolkit' {
9797
export interface SliceReducerCreators<
98-
State = any,
99-
CaseReducers extends
100-
CreatorCaseReducers<State> = CreatorCaseReducers<State>,
101-
Name extends string = string,
98+
State,
99+
CaseReducers extends CreatorCaseReducers<State>,
100+
Name extends string,
102101
> {
103102
[entityMethodsCreatorType]: ReducerCreatorEntry<EntityMethodsCreator<State>>
104103
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,10 +1086,9 @@ interface AddToastThunk<Name extends string, ReducerName extends PropertyKey> {
10861086

10871087
declare module '@reduxjs/toolkit' {
10881088
export interface SliceReducerCreators<
1089-
State = any,
1090-
CaseReducers extends
1091-
CreatorCaseReducers<State> = CreatorCaseReducers<State>,
1092-
Name extends string = string,
1089+
State,
1090+
CaseReducers extends CreatorCaseReducers<State>,
1091+
Name extends string,
10931092
> {
10941093
[toasterCreatorType]: ReducerCreatorEntry<
10951094
State extends ToastState

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,10 +1244,9 @@ interface UndoableOptions {
12441244

12451245
declare module '@reduxjs/toolkit' {
12461246
export interface SliceReducerCreators<
1247-
State = any,
1248-
CaseReducers extends
1249-
CreatorCaseReducers<State> = CreatorCaseReducers<State>,
1250-
Name extends string = string,
1247+
State,
1248+
CaseReducers extends CreatorCaseReducers<State>,
1249+
Name extends string,
12511250
> {
12521251
[loaderCreatorType]: ReducerCreatorEntry<
12531252
(

0 commit comments

Comments
 (0)