@@ -3,6 +3,8 @@ import type {EmptyObject} from './empty-object';
3
3
import type { IsAny } from './is-any' ;
4
4
import type { IsNever } from './is-never' ;
5
5
import type { UnknownArray } from './unknown-array' ;
6
+ import type { Sum } from './sum' ;
7
+ import type { LessThan } from './less-than' ;
6
8
7
9
/**
8
10
Generate a union of all possible paths to properties in the given object.
@@ -45,22 +47,24 @@ open('listB.1'); // TypeError. Because listB only has one element.
45
47
@category Object
46
48
@category Array
47
49
*/
48
- export type Paths < T > =
50
+ export type Paths < T > = Paths_ < T > ;
51
+
52
+ type Paths_ < T , Depth extends number = 0 > =
49
53
T extends NonRecursiveType | ReadonlyMap < unknown , unknown > | ReadonlySet < unknown >
50
54
? never
51
55
: IsAny < T > extends true
52
56
? never
53
57
: T extends UnknownArray
54
58
? number extends T [ 'length' ]
55
59
// We need to handle the fixed and non-fixed index part of the array separately.
56
- ? InternalPaths < StaticPartOfArray < T > >
57
- | InternalPaths < Array < VariablePartOfArray < T > [ number ] > >
58
- : InternalPaths < T >
60
+ ? InternalPaths < StaticPartOfArray < T > , Depth >
61
+ | InternalPaths < Array < VariablePartOfArray < T > [ number ] > , Depth >
62
+ : InternalPaths < T , Depth >
59
63
: T extends object
60
- ? InternalPaths < T >
64
+ ? InternalPaths < T , Depth >
61
65
: never ;
62
66
63
- export type InternalPaths < _T , T = Required < _T > > =
67
+ export type InternalPaths < _T , Depth extends number = 0 , T = Required < _T > > =
64
68
T extends EmptyObject | readonly [ ]
65
69
? never
66
70
: {
@@ -71,8 +75,10 @@ export type InternalPaths<_T, T = Required<_T>> =
71
75
| Key
72
76
| ToString < Key >
73
77
| (
74
- IsNever < Paths < T [ Key ] > > extends false
75
- ? `${Key } .${Paths < T [ Key ] > } `
78
+ LessThan < Depth , 15 > extends true // Limit the depth to prevent infinite recursion
79
+ ? IsNever < Paths_ < T [ Key ] , Sum < Depth , 1 > > > extends false
80
+ ? `${Key } .${Paths_ < T [ Key ] , Sum < Depth , 1 > > } `
81
+ : never
76
82
: never
77
83
)
78
84
: never
0 commit comments