Skip to content

Commit 2cb7e47

Browse files
committed
expose Name and ReducerPath in types, and add test for selectSlice
1 parent e3b8d90 commit 2cb7e47

File tree

3 files changed

+201
-37
lines changed

3 files changed

+201
-37
lines changed

packages/toolkit/src/createSlice.ts

Lines changed: 71 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@ export enum ReducerType {
3232
asyncThunk = 'asyncThunk',
3333
}
3434

35-
export type RegisteredReducerType = keyof SliceReducerCreators<any, any, any>
35+
export type RegisteredReducerType = keyof SliceReducerCreators<
36+
any,
37+
any,
38+
any,
39+
any
40+
>
3641

3742
export interface ReducerDefinition<
3843
T extends RegisteredReducerType = RegisteredReducerType,
@@ -64,6 +69,7 @@ export interface SliceReducerCreators<
6469
State,
6570
CaseReducers extends CreatorCaseReducers<State>,
6671
Name extends string,
72+
ReducerPath extends string,
6773
> {
6874
[ReducerType.reducer]: ReducerCreatorEntry<
6975
{
@@ -164,22 +170,36 @@ export interface SliceReducerCreators<
164170

165171
export type ReducerCreators<
166172
State,
173+
Name extends string = string,
174+
ReducerPath extends string = Name,
167175
CreatorMap extends Record<string, RegisteredReducerType> = {},
168176
> = {
169-
reducer: SliceReducerCreators<State, any, any>[ReducerType.reducer]['create']
177+
reducer: SliceReducerCreators<
178+
State,
179+
any,
180+
Name,
181+
ReducerPath
182+
>[ReducerType.reducer]['create']
170183
preparedReducer: SliceReducerCreators<
171184
State,
172185
any,
173-
any
186+
Name,
187+
ReducerPath
174188
>[ReducerType.reducerWithPrepare]['create']
175189
} & {
176-
[Name in keyof CreatorMap as SliceReducerCreators<
190+
[CreatorName in keyof CreatorMap as SliceReducerCreators<
177191
State,
178192
any,
179-
any
180-
>[CreatorMap[Name]]['create'] extends never
193+
Name,
194+
ReducerPath
195+
>[CreatorMap[CreatorName]]['create'] extends never
181196
? never
182-
: Name]: SliceReducerCreators<State, any, any>[CreatorMap[Name]]['create']
197+
: CreatorName]: SliceReducerCreators<
198+
State,
199+
any,
200+
Name,
201+
ReducerPath
202+
>[CreatorMap[CreatorName]]['create']
183203
}
184204

185205
interface InternalReducerHandlingContext<State> {
@@ -192,8 +212,8 @@ interface InternalReducerHandlingContext<State> {
192212

193213
export interface ReducerHandlingContext<
194214
State,
195-
Name extends string,
196-
ReducerPath extends string,
215+
Name extends string = string,
216+
ReducerPath extends string = Name,
197217
> {
198218
/**
199219
* Adds a case reducer to handle a single action type.
@@ -317,15 +337,19 @@ type RecursiveExtractDefinition<
317337
type ReducerDefinitionsForType<Type extends RegisteredReducerType> = {
318338
[CreatorType in RegisteredReducerType]:
319339
| RecursiveExtractDefinition<
320-
ReturnType<SliceReducerCreators<any, any, any>[CreatorType]['create']>,
340+
ReturnType<
341+
SliceReducerCreators<any, any, any, any>[CreatorType]['create']
342+
>,
321343
Type
322344
>
323345
| {
324346
[K in keyof SliceReducerCreators<
347+
any,
325348
any,
326349
any,
327350
any
328351
>[CreatorType]['create']]: SliceReducerCreators<
352+
any,
329353
any,
330354
any,
331355
any
@@ -334,12 +358,12 @@ type ReducerDefinitionsForType<Type extends RegisteredReducerType> = {
334358
) => infer Definitions
335359
? RecursiveExtractDefinition<Definitions, Type>
336360
: never
337-
}[keyof SliceReducerCreators<any, any, any>[CreatorType]['create']]
338-
}[keyof SliceReducerCreators<any, any, any>]
361+
}[keyof SliceReducerCreators<any, any, any, any>[CreatorType]['create']]
362+
}[RegisteredReducerType]
339363

340364
export type ReducerCreator<Type extends RegisteredReducerType> = {
341365
type: Type
342-
create: SliceReducerCreators<any, any, any>[Type]['create']
366+
create: SliceReducerCreators<any, any, any, any>[Type]['create']
343367
} & (ReducerDefinitionsForType<Type> extends never
344368
? {}
345369
: {
@@ -385,13 +409,13 @@ export interface Slice<
385409
* Action creators for the types of actions that are handled by the slice
386410
* reducer.
387411
*/
388-
actions: CaseReducerActions<CaseReducers, Name, State>
412+
actions: CaseReducerActions<CaseReducers, Name, ReducerPath, State>
389413

390414
/**
391415
* The individual case reducer functions that were passed in the `reducers` parameter.
392416
* This enables reuse and testing if they were defined inline when calling `createSlice`.
393417
*/
394-
caseReducers: SliceDefinedCaseReducers<CaseReducers, Name, State>
418+
caseReducers: SliceDefinedCaseReducers<CaseReducers, Name, ReducerPath, State>
395419

396420
/**
397421
* Provides access to the initial state value given to the slice.
@@ -492,16 +516,25 @@ interface InjectedSlice<
492516

493517
type CreatorCallback<
494518
State,
519+
Name extends string,
520+
ReducerPath extends string,
495521
CreatorMap extends Record<string, RegisteredReducerType>,
496522
> = (
497-
create: ReducerCreators<State, CreatorMap>,
523+
create: ReducerCreators<State, Name, ReducerPath, CreatorMap>,
498524
) => Record<string, ReducerDefinition>
499525

500526
type GetCaseReducers<
501527
State,
528+
Name extends string,
529+
ReducerPath extends string,
502530
CreatorMap extends Record<string, RegisteredReducerType>,
503-
CR extends SliceCaseReducers<State> | CreatorCallback<State, CreatorMap>,
504-
> = CR extends CreatorCallback<State, CreatorMap> ? ReturnType<CR> : CR
531+
CR extends
532+
| SliceCaseReducers<State>
533+
| CreatorCallback<State, Name, ReducerPath, CreatorMap>,
534+
> =
535+
CR extends CreatorCallback<State, Name, ReducerPath, CreatorMap>
536+
? ReturnType<CR>
537+
: CR
505538

506539
/**
507540
* Options for `createSlice()`.
@@ -512,7 +545,12 @@ export interface CreateSliceOptions<
512545
State = any,
513546
CR extends
514547
| SliceCaseReducers<State>
515-
| CreatorCallback<State, CreatorMap> = SliceCaseReducers<State>,
548+
| CreatorCallback<
549+
State,
550+
Name,
551+
ReducerPath,
552+
CreatorMap
553+
> = SliceCaseReducers<State>,
516554
Name extends string = string,
517555
ReducerPath extends string = Name,
518556
Selectors extends SliceSelectors<State> = SliceSelectors<State>,
@@ -658,14 +696,16 @@ type ConvertNeverKeysToUnknown<T> = T extends any
658696
export type CaseReducerActions<
659697
CaseReducers extends CreatorCaseReducers<State>,
660698
SliceName extends string,
699+
ReducerPath extends string = SliceName,
661700
State = any,
662701
> = Id<
663702
UnionToIntersection<
664703
ConvertNeverKeysToUnknown<
665704
SliceReducerCreators<
666705
State,
667706
CaseReducers,
668-
SliceName
707+
SliceName,
708+
ReducerPath
669709
>[RegisteredReducerType]['actions']
670710
>
671711
>
@@ -704,14 +744,16 @@ type ActionCreatorForCaseReducer<CR, Type extends string> = CR extends (
704744
type SliceDefinedCaseReducers<
705745
CaseReducers extends CreatorCaseReducers<State>,
706746
SliceName extends string = string,
747+
ReducerPath extends string = SliceName,
707748
State = any,
708749
> = Id<
709750
UnionToIntersection<
710751
ConvertNeverKeysToUnknown<
711752
SliceReducerCreators<
712753
State,
713754
CaseReducers,
714-
SliceName
755+
SliceName,
756+
ReducerPath
715757
>[RegisteredReducerType]['caseReducers']
716758
>
717759
>
@@ -752,7 +794,7 @@ type SliceDefinedSelectors<
752794
*/
753795
export type ValidateSliceCaseReducers<
754796
S,
755-
ACR extends SliceCaseReducers<S> | CreatorCallback<S, any>,
797+
ACR extends SliceCaseReducers<S> | CreatorCallback<S, any, any, any>,
756798
> = ACR & {
757799
[T in keyof ACR]: ACR[T] extends {
758800
reducer(s: S, action?: infer A): any
@@ -809,12 +851,9 @@ export const preparedReducerCreator: ReducerCreator<ReducerType.reducerWithPrepa
809851
},
810852
}
811853

812-
const isCreatorCallback = <
813-
State,
814-
CreatorMap extends Record<string, RegisteredReducerType>,
815-
>(
816-
reducers: any,
817-
): reducers is CreatorCallback<State, CreatorMap> =>
854+
const isCreatorCallback = (
855+
reducers: unknown,
856+
): reducers is CreatorCallback<any, any, any, any> =>
818857
typeof reducers === 'function'
819858

820859
interface BuildCreateSliceConfig<
@@ -881,7 +920,7 @@ export function buildCreateSlice<
881920
State,
882921
CaseReducers extends
883922
| SliceCaseReducers<State>
884-
| CreatorCallback<State, CreatorMap>,
923+
| CreatorCallback<State, Name, ReducerPath, CreatorMap>,
885924
Name extends string,
886925
Selectors extends SliceSelectors<State>,
887926
ReducerPath extends string = Name,
@@ -896,7 +935,7 @@ export function buildCreateSlice<
896935
>,
897936
): Slice<
898937
State,
899-
GetCaseReducers<State, CreatorMap, CaseReducers>,
938+
GetCaseReducers<State, Name, ReducerPath, CreatorMap, CaseReducers>,
900939
Name,
901940
ReducerPath,
902941
Selectors
@@ -1097,7 +1136,7 @@ export function buildCreateSlice<
10971136
): Pick<
10981137
Slice<
10991138
State,
1100-
GetCaseReducers<State, CreatorMap, CaseReducers>,
1139+
GetCaseReducers<State, Name, ReducerPath, CreatorMap, CaseReducers>,
11011140
Name,
11021141
CurrentReducerPath,
11031142
Selectors
@@ -1153,7 +1192,7 @@ export function buildCreateSlice<
11531192

11541193
const slice: Slice<
11551194
State,
1156-
CaseReducers extends CreatorCallback<State, CreatorMap>
1195+
CaseReducers extends CreatorCallback<State, Name, ReducerPath, CreatorMap>
11571196
? ReturnType<CaseReducers>
11581197
: CaseReducers,
11591198
Name,

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,7 @@ describe('type tests', () => {
796796
create: () => ({
797797
_reducerDefinitionType: toasterCreatorType,
798798
}),
799-
handle({ type, reducerName }, _definition, context) {
799+
handle({ type }, _definition, context) {
800800
const toastOpened = createAction<{ message: string; id: string }>(
801801
type + '/opened',
802802
)
@@ -886,6 +886,7 @@ declare module '@reduxjs/toolkit' {
886886
State,
887887
CaseReducers extends CreatorCaseReducers<State>,
888888
Name extends string,
889+
ReducerPath extends string,
889890
> {
890891
[toasterCreatorType]: ReducerCreatorEntry<
891892
State extends ToastState

0 commit comments

Comments
 (0)