Skip to content

Commit 32cdde8

Browse files
committed
limit recursion depth of definition extraction
1 parent ce04544 commit 32cdde8

File tree

2 files changed

+61
-40
lines changed

2 files changed

+61
-40
lines changed

packages/toolkit/src/createSlice.ts

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,15 @@ import type {
1616
import { createReducer, makeGetInitialState } from './createReducer'
1717
import type { ActionReducerMapBuilder, TypedActionCreator } from './mapBuilders'
1818
import { 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'
2028
import type { InjectConfig } from './combineSlices'
2129
import { 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

284300
type 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

311307
export 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,

packages/toolkit/src/tsHelpers.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,3 +223,22 @@ export function asSafePromise<Resolved, Rejected>(
223223
) {
224224
return promise.catch(fallback) as SafePromise<Resolved | Rejected>
225225
}
226+
227+
export type OverloadedReturnType<Fn extends (...args: any[]) => any> =
228+
Fn extends {
229+
(...args: any): infer R1
230+
(...args: any): infer R2
231+
(...args: any): infer R3
232+
}
233+
? R1 | R2 | R3
234+
: Fn extends {
235+
(...args: any): infer R1
236+
(...args: any): infer R2
237+
}
238+
? R1 | R2
239+
: ReturnType<Fn>
240+
241+
export type Increment<
242+
N extends number,
243+
Acc extends 0[] = [],
244+
> = Acc['length'] extends N ? [...Acc, 0]['length'] : Increment<N, [...Acc, 0]>

0 commit comments

Comments
 (0)