1 | import type {BuildObject, BuildTuple, NonRecursiveType, ObjectValue} from './internal';
|
2 | import type {IsNever} from './is-never';
|
3 | import type {Paths} from './paths';
|
4 | import type {Simplify} from './simplify.d';
|
5 | import type {UnionToIntersection} from './union-to-intersection.d';
|
6 | import type {UnknownArray} from './unknown-array';
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 | export type PickDeep<T, PathUnion extends Paths<T>> =
|
80 | T extends NonRecursiveType
|
81 | ? never
|
82 | : T extends UnknownArray
|
83 | ? UnionToIntersection<{
|
84 | [P in PathUnion]: InternalPickDeep<T, P>;
|
85 | }[PathUnion]
|
86 | >
|
87 | : T extends object
|
88 | ? Simplify<UnionToIntersection<{
|
89 | [P in PathUnion]: InternalPickDeep<T, P>;
|
90 | }[PathUnion]>>
|
91 | : never;
|
92 |
|
93 |
|
94 |
|
95 |
|
96 | type InternalPickDeep<T, Path extends string | number> =
|
97 | T extends NonRecursiveType
|
98 | ? never
|
99 | : T extends UnknownArray ? PickDeepArray<T, Path>
|
100 | : T extends object ? Simplify<PickDeepObject<T, Path>>
|
101 | : never;
|
102 |
|
103 |
|
104 |
|
105 |
|
106 | type PickDeepObject<RecordType extends object, P extends string | number> =
|
107 | P extends `${infer RecordKeyInPath}.${infer SubPath}`
|
108 | ? ObjectValue<RecordType, RecordKeyInPath> extends infer ObjectV
|
109 | ? IsNever<ObjectV> extends false
|
110 | ? BuildObject<RecordKeyInPath, InternalPickDeep<NonNullable<ObjectV>, SubPath>, RecordType>
|
111 | : never
|
112 | : never
|
113 | : ObjectValue<RecordType, P> extends infer ObjectV
|
114 | ? IsNever<ObjectV> extends false
|
115 | ? BuildObject<P, ObjectV, RecordType>
|
116 | : never
|
117 | : never;
|
118 |
|
119 |
|
120 |
|
121 |
|
122 | type PickDeepArray<ArrayType extends UnknownArray, P extends string | number> =
|
123 |
|
124 | P extends `${infer ArrayIndex extends number}.${infer SubPath}`
|
125 |
|
126 | ? number extends ArrayIndex
|
127 | ? ArrayType extends unknown[]
|
128 | ? Array<InternalPickDeep<NonNullable<ArrayType[number]>, SubPath>>
|
129 | : ArrayType extends readonly unknown[]
|
130 | ? ReadonlyArray<InternalPickDeep<NonNullable<ArrayType[number]>, SubPath>>
|
131 | : never
|
132 |
|
133 | : ArrayType extends unknown[]
|
134 | ? [...BuildTuple<ArrayIndex>, InternalPickDeep<NonNullable<ArrayType[ArrayIndex]>, SubPath>]
|
135 | : ArrayType extends readonly unknown[]
|
136 | ? readonly [...BuildTuple<ArrayIndex>, InternalPickDeep<NonNullable<ArrayType[ArrayIndex]>, SubPath>]
|
137 | : never
|
138 |
|
139 | : P extends `${infer ArrayIndex extends number}`
|
140 |
|
141 | ? number extends ArrayIndex
|
142 | ? ArrayType
|
143 |
|
144 | : ArrayType extends unknown[]
|
145 | ? [...BuildTuple<ArrayIndex>, ArrayType[ArrayIndex]]
|
146 | : ArrayType extends readonly unknown[]
|
147 | ? readonly [...BuildTuple<ArrayIndex>, ArrayType[ArrayIndex]]
|
148 | : never
|
149 | : never;
|