@@ -16,7 +16,15 @@ import type {
1616import { createReducer , makeGetInitialState } from './createReducer'
1717import type { ActionReducerMapBuilder , TypedActionCreator } from './mapBuilders'
1818import { executeReducerBuilderCallback } from './mapBuilders'
19- import type { CastAny , Id , TypeGuard , UnionToIntersection } from './tsHelpers'
19+ import type {
20+ CastAny ,
21+ Id ,
22+ Increment ,
23+ IsAny ,
24+ OverloadedReturnType ,
25+ TypeGuard ,
26+ UnionToIntersection ,
27+ } from './tsHelpers'
2028import type { InjectConfig } from './combineSlices'
2129import { emplace } from './utils'
2230
@@ -265,54 +273,48 @@ export interface ReducerDetails {
265273 type : string
266274}
267275
268- type RecursiveExtractDefinition <
269- Definitions ,
276+ type DefinitionFromValue <
277+ T extends object ,
270278 Type extends RegisteredReducerType ,
271- > = CastAny <
272- | Extract < Definitions , ReducerDefinition < Type > >
273- | ( Definitions extends object
274- ? {
275- [ K in keyof Definitions ] -?: RecursiveExtractDefinition <
276- Definitions [ K ] ,
277- Type
278- >
279- } [ keyof Definitions ]
280- : never ) ,
281- never
282- >
279+ RecursionDepth extends number = 0 ,
280+ > = RecursionDepth extends 5
281+ ? never
282+ : IsAny <
283+ T ,
284+ never ,
285+ | Extract < T , ReducerDefinition < Type > >
286+ | {
287+ [ K in keyof T ] -?: T [ K ] extends object
288+ ? DefinitionFromValue < T [ K ] , Type , Increment < RecursionDepth > >
289+ : never
290+ } [ keyof T ]
291+ | ( T extends ( ...args : any [ ] ) => object
292+ ? DefinitionFromValue <
293+ OverloadedReturnType < T > ,
294+ Type ,
295+ Increment < RecursionDepth >
296+ >
297+ : never )
298+ >
283299
284300type ReducerDefinitionsForType < Type extends RegisteredReducerType > = {
285- [ CreatorType in RegisteredReducerType ] :
286- | RecursiveExtractDefinition <
287- ReturnType <
288- SliceReducerCreators < any , any , any , any > [ CreatorType ] [ 'create' ]
289- > ,
290- Type
291- >
292- | {
293- [ K in keyof SliceReducerCreators <
294- any ,
295- any ,
296- any ,
297- any
298- > [ CreatorType ] [ 'create' ] ] : SliceReducerCreators <
299- any ,
300- any ,
301- any ,
302- any
303- > [ CreatorType ] [ 'create' ] [ K ] extends (
304- ...args : any [ ]
305- ) => infer Definitions
306- ? RecursiveExtractDefinition < Definitions , Type >
307- : never
308- } [ keyof SliceReducerCreators < any , any , any , any > [ CreatorType ] [ 'create' ] ]
301+ [ CreatorType in RegisteredReducerType ] : DefinitionFromValue <
302+ SliceReducerCreators < any , any , any , any > [ CreatorType ] [ 'create' ] ,
303+ Type
304+ >
309305} [ RegisteredReducerType ]
310306
311307export type ReducerCreator < Type extends RegisteredReducerType > = {
312308 type : Type
313309 create : SliceReducerCreators < any , any , any , any > [ Type ] [ 'create' ]
314310} & ( ReducerDefinitionsForType < Type > extends never
315- ? { }
311+ ? {
312+ handle ?< State > (
313+ details : ReducerDetails ,
314+ definition : unknown ,
315+ context : ReducerHandlingContext < State > ,
316+ ) : void
317+ }
316318 : {
317319 handle < State > (
318320 details : ReducerDetails ,
0 commit comments