1 | import type {ConditionalSimplifyDeep} from './conditional-simplify';
|
2 | import type {OmitIndexSignature} from './omit-index-signature';
|
3 | import type {PickIndexSignature} from './pick-index-signature';
|
4 | import type {Merge} from './merge';
|
5 | import type {
|
6 | FirstArrayElement,
|
7 | IsBothExtends,
|
8 | UnknownArrayOrTuple,
|
9 | } from './internal';
|
10 | import type {NonEmptyTuple} from './non-empty-tuple';
|
11 | import type {ArrayTail} from './array-tail';
|
12 | import type {UnknownRecord} from './unknown-record';
|
13 | import type {EnforceOptional} from './enforce-optional';
|
14 | import type {SimplifyDeep} from './simplify-deep';
|
15 | import type {UnknownArray} from './unknown-array';
|
16 |
|
17 | type SimplifyDeepExcludeArray<T> = SimplifyDeep<T, UnknownArray>;
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | type MergeDeepRecordProperty<
|
23 | Destination,
|
24 | Source,
|
25 | Options extends MergeDeepInternalOptions,
|
26 | > = undefined extends Source
|
27 | ? MergeDeepOrReturn<Source, Exclude<Destination, undefined>, Exclude<Source, undefined>, Options> | undefined
|
28 | : MergeDeepOrReturn<Source, Destination, Source, Options>;
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | type DoMergeDeepRecord<
|
38 | Destination extends UnknownRecord,
|
39 | Source extends UnknownRecord,
|
40 | Options extends MergeDeepInternalOptions,
|
41 | > =
|
42 |
|
43 | {
|
44 | [Key in keyof Destination as Key extends keyof Source ? never : Key]: Destination[Key];
|
45 | }
|
46 |
|
47 | & {
|
48 | [Key in keyof Source as Key extends keyof Destination ? never : Key]: Source[Key];
|
49 | }
|
50 |
|
51 | & {
|
52 | [Key in keyof Source as Key extends keyof Destination ? Key : never]: MergeDeepRecordProperty<Destination[Key], Source[Key], Options>;
|
53 | };
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | type MergeDeepRecord<
|
59 | Destination extends UnknownRecord,
|
60 | Source extends UnknownRecord,
|
61 | Options extends MergeDeepInternalOptions,
|
62 | > = DoMergeDeepRecord<OmitIndexSignature<Destination>, OmitIndexSignature<Source>, Options>
|
63 | & Merge<PickIndexSignature<Destination>, PickIndexSignature<Source>>;
|
64 |
|
65 |
|
66 | type PickRestTypeHelper<Tail extends UnknownArrayOrTuple, Type> = Tail extends [] ? Type : PickRestType<Tail>;
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 | type PickRestType<Type extends UnknownArrayOrTuple> = number extends Type['length']
|
81 | ? PickRestTypeHelper<ArrayTail<Type>, Type>
|
82 | : [];
|
83 |
|
84 |
|
85 | type OmitRestTypeHelper<
|
86 | Tail extends UnknownArrayOrTuple,
|
87 | Type extends UnknownArrayOrTuple,
|
88 | Result extends UnknownArrayOrTuple = [],
|
89 | > = Tail extends []
|
90 | ? Result
|
91 | : OmitRestType<Tail, [...Result, FirstArrayElement<Type>]>;
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 | type OmitRestType<Type extends UnknownArrayOrTuple, Result extends UnknownArrayOrTuple = []> = number extends Type['length']
|
107 | ? OmitRestTypeHelper<ArrayTail<Type>, Type, Result>
|
108 | : Type;
|
109 |
|
110 |
|
111 | type TypeNumberOrType<Type extends UnknownArrayOrTuple> = Type[number] extends never ? Type : Type[number];
|
112 |
|
113 |
|
114 | type PickRestTypeFlat<Type extends UnknownArrayOrTuple> = TypeNumberOrType<PickRestType<Type>>;
|
115 |
|
116 |
|
117 |
|
118 |
|
119 | type MergeDeepArrayOrTupleElements<
|
120 | Destination,
|
121 | Source,
|
122 | Options extends MergeDeepInternalOptions,
|
123 | > = Source extends []
|
124 | ? Destination
|
125 | : Destination extends []
|
126 | ? Source
|
127 | : MergeDeepOrReturn<Source, Destination, Source, Options>;
|
128 |
|
129 |
|
130 |
|
131 |
|
132 | type DoMergeDeepTupleAndTupleRecursive<
|
133 | Destination extends UnknownArrayOrTuple,
|
134 | Source extends UnknownArrayOrTuple,
|
135 | DestinationRestType,
|
136 | SourceRestType,
|
137 | Options extends MergeDeepInternalOptions,
|
138 | > = Destination extends []
|
139 | ? Source extends []
|
140 | ? []
|
141 | : MergeArrayTypeAndTuple<DestinationRestType, Source, Options>
|
142 | : Source extends []
|
143 | ? MergeTupleAndArrayType<Destination, SourceRestType, Options>
|
144 | : [
|
145 | MergeDeepArrayOrTupleElements<FirstArrayElement<Destination>, FirstArrayElement<Source>, Options>,
|
146 | ...DoMergeDeepTupleAndTupleRecursive<ArrayTail<Destination>, ArrayTail<Source>, DestinationRestType, SourceRestType, Options>,
|
147 | ];
|
148 |
|
149 |
|
150 |
|
151 |
|
152 | type MergeDeepTupleAndTupleRecursive<
|
153 | Destination extends UnknownArrayOrTuple,
|
154 | Source extends UnknownArrayOrTuple,
|
155 | Options extends MergeDeepInternalOptions,
|
156 | > = [
|
157 | ...DoMergeDeepTupleAndTupleRecursive<OmitRestType<Destination>, OmitRestType<Source>, PickRestTypeFlat<Destination>, PickRestTypeFlat<Source>, Options>,
|
158 | ...MergeDeepArrayOrTupleElements<PickRestType<Destination>, PickRestType<Source>, Options>,
|
159 | ];
|
160 |
|
161 |
|
162 |
|
163 |
|
164 | type MergeTupleAndArrayType<
|
165 | Tuple extends UnknownArrayOrTuple,
|
166 | ArrayType,
|
167 | Options extends MergeDeepInternalOptions,
|
168 | > = Tuple extends []
|
169 | ? Tuple
|
170 | : [
|
171 | MergeDeepArrayOrTupleElements<FirstArrayElement<Tuple>, ArrayType, Options>,
|
172 | ...MergeTupleAndArrayType<ArrayTail<Tuple>, ArrayType, Options>,
|
173 | ];
|
174 |
|
175 |
|
176 |
|
177 |
|
178 | type MergeDeepTupleAndArrayRecursive<
|
179 | Destination extends UnknownArrayOrTuple,
|
180 | Source extends UnknownArrayOrTuple,
|
181 | Options extends MergeDeepInternalOptions,
|
182 | > = [
|
183 | ...MergeTupleAndArrayType<OmitRestType<Destination>, Source[number], Options>,
|
184 | ...MergeDeepArrayOrTupleElements<PickRestType<Destination>, PickRestType<Source>, Options>,
|
185 | ];
|
186 |
|
187 |
|
188 |
|
189 |
|
190 | type MergeArrayTypeAndTuple<
|
191 | ArrayType,
|
192 | Tuple extends UnknownArrayOrTuple,
|
193 | Options extends MergeDeepInternalOptions,
|
194 | > = Tuple extends []
|
195 | ? Tuple
|
196 | : [
|
197 | MergeDeepArrayOrTupleElements<ArrayType, FirstArrayElement<Tuple>, Options>,
|
198 | ...MergeArrayTypeAndTuple<ArrayType, ArrayTail<Tuple>, Options>,
|
199 | ];
|
200 |
|
201 |
|
202 |
|
203 |
|
204 | type MergeDeepArrayAndTupleRecursive<
|
205 | Destination extends UnknownArrayOrTuple,
|
206 | Source extends UnknownArrayOrTuple,
|
207 | Options extends MergeDeepInternalOptions,
|
208 | > = [
|
209 | ...MergeArrayTypeAndTuple<Destination[number], OmitRestType<Source>, Options>,
|
210 | ...MergeDeepArrayOrTupleElements<PickRestType<Destination>, PickRestType<Source>, Options>,
|
211 | ];
|
212 |
|
213 |
|
214 |
|
215 |
|
216 | type ArrayMergeMode = 'spread' | 'replace';
|
217 |
|
218 |
|
219 |
|
220 |
|
221 | type ShouldSpread<Options extends MergeDeepInternalOptions> = Options['spreadTopLevelArrays'] extends false
|
222 | ? Options['arrayMergeMode'] extends 'spread' ? true : false
|
223 | : true;
|
224 |
|
225 |
|
226 |
|
227 |
|
228 | type DoMergeArrayOrTuple<
|
229 | Destination extends UnknownArrayOrTuple,
|
230 | Source extends UnknownArrayOrTuple,
|
231 | Options extends MergeDeepInternalOptions,
|
232 | > = ShouldSpread<Options> extends true
|
233 | ? Array<Exclude<Destination, undefined>[number] | Exclude<Source, undefined>[number]>
|
234 | : Source;
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 | type MergeDeepArrayRecursive<
|
244 | Destination extends UnknownArrayOrTuple,
|
245 | Source extends UnknownArrayOrTuple,
|
246 | Options extends MergeDeepInternalOptions,
|
247 | > = Destination[number] extends UnknownArrayOrTuple
|
248 | ? Source[number] extends UnknownArrayOrTuple
|
249 | ? Array<MergeDeepArrayOrTupleRecursive<Destination[number], Source[number], Options>>
|
250 | : DoMergeArrayOrTuple<Destination, Source, Options>
|
251 | : Destination[number] extends UnknownRecord
|
252 | ? Source[number] extends UnknownRecord
|
253 | ? Array<SimplifyDeepExcludeArray<MergeDeepRecord<Destination[number], Source[number], Options>>>
|
254 | : DoMergeArrayOrTuple<Destination, Source, Options>
|
255 | : DoMergeArrayOrTuple<Destination, Source, Options>;
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 | type MergeDeepArrayOrTupleRecursive<
|
266 | Destination extends UnknownArrayOrTuple,
|
267 | Source extends UnknownArrayOrTuple,
|
268 | Options extends MergeDeepInternalOptions,
|
269 | > = IsBothExtends<NonEmptyTuple, Destination, Source> extends true
|
270 | ? MergeDeepTupleAndTupleRecursive<Destination, Source, Options>
|
271 | : Destination extends NonEmptyTuple
|
272 | ? MergeDeepTupleAndArrayRecursive<Destination, Source, Options>
|
273 | : Source extends NonEmptyTuple
|
274 | ? MergeDeepArrayAndTupleRecursive<Destination, Source, Options>
|
275 | : MergeDeepArrayRecursive<Destination, Source, Options>;
|
276 |
|
277 |
|
278 |
|
279 |
|
280 | type MergeDeepArrayOrTuple<
|
281 | Destination extends UnknownArrayOrTuple,
|
282 | Source extends UnknownArrayOrTuple,
|
283 | Options extends MergeDeepInternalOptions,
|
284 | > = Options['recurseIntoArrays'] extends true
|
285 | ? MergeDeepArrayOrTupleRecursive<Destination, Source, Options>
|
286 | : DoMergeArrayOrTuple<Destination, Source, Options>;
|
287 |
|
288 |
|
289 |
|
290 |
|
291 | type MergeDeepOrReturn<
|
292 | DefaultType,
|
293 | Destination,
|
294 | Source,
|
295 | Options extends MergeDeepInternalOptions,
|
296 | > = SimplifyDeepExcludeArray<[undefined] extends [Destination | Source]
|
297 | ? DefaultType
|
298 | : Destination extends UnknownRecord
|
299 | ? Source extends UnknownRecord
|
300 | ? MergeDeepRecord<Destination, Source, Options>
|
301 | : DefaultType
|
302 | : Destination extends UnknownArrayOrTuple
|
303 | ? Source extends UnknownArrayOrTuple
|
304 | ? MergeDeepArrayOrTuple<Destination, Source, EnforceOptional<Merge<Options, {spreadTopLevelArrays: false}>>>
|
305 | : DefaultType
|
306 | : DefaultType>;
|
307 |
|
308 |
|
309 |
|
310 |
|
311 |
|
312 |
|
313 | export type MergeDeepOptions = {
|
314 | |
315 |
|
316 |
|
317 |
|
318 |
|
319 |
|
320 |
|
321 |
|
322 |
|
323 |
|
324 |
|
325 |
|
326 |
|
327 | arrayMergeMode?: ArrayMergeMode;
|
328 |
|
329 | |
330 |
|
331 |
|
332 |
|
333 |
|
334 |
|
335 |
|
336 |
|
337 |
|
338 |
|
339 | recurseIntoArrays?: boolean;
|
340 | };
|
341 |
|
342 |
|
343 |
|
344 |
|
345 | type MergeDeepInternalOptions = Merge<MergeDeepOptions, {spreadTopLevelArrays?: boolean}>;
|
346 |
|
347 |
|
348 |
|
349 |
|
350 | type DefaultMergeDeepOptions<Options extends MergeDeepOptions> = Merge<{
|
351 | arrayMergeMode: 'replace';
|
352 | recurseIntoArrays: false;
|
353 | spreadTopLevelArrays: true;
|
354 | }, Options>;
|
355 |
|
356 |
|
357 |
|
358 |
|
359 | type MergeDeepWithDefaultOptions<Destination, Source, Options extends MergeDeepOptions> = SimplifyDeepExcludeArray<
|
360 | [undefined] extends [Destination | Source]
|
361 | ? never
|
362 | : Destination extends UnknownRecord
|
363 | ? Source extends UnknownRecord
|
364 | ? MergeDeepRecord<Destination, Source, DefaultMergeDeepOptions<Options>>
|
365 | : never
|
366 | : Destination extends UnknownArrayOrTuple
|
367 | ? Source extends UnknownArrayOrTuple
|
368 | ? MergeDeepArrayOrTuple<Destination, Source, DefaultMergeDeepOptions<Options>>
|
369 | : never
|
370 | : never
|
371 | >;
|
372 |
|
373 |
|
374 |
|
375 |
|
376 |
|
377 |
|
378 |
|
379 |
|
380 |
|
381 |
|
382 |
|
383 |
|
384 |
|
385 |
|
386 |
|
387 |
|
388 |
|
389 |
|
390 |
|
391 |
|
392 |
|
393 |
|
394 |
|
395 |
|
396 |
|
397 |
|
398 |
|
399 |
|
400 |
|
401 |
|
402 |
|
403 |
|
404 |
|
405 |
|
406 |
|
407 |
|
408 |
|
409 |
|
410 |
|
411 |
|
412 |
|
413 |
|
414 |
|
415 |
|
416 |
|
417 |
|
418 |
|
419 |
|
420 |
|
421 |
|
422 |
|
423 |
|
424 |
|
425 |
|
426 |
|
427 |
|
428 |
|
429 |
|
430 |
|
431 |
|
432 |
|
433 |
|
434 |
|
435 |
|
436 |
|
437 |
|
438 |
|
439 |
|
440 |
|
441 |
|
442 |
|
443 |
|
444 |
|
445 |
|
446 |
|
447 |
|
448 |
|
449 |
|
450 |
|
451 |
|
452 |
|
453 |
|
454 |
|
455 |
|
456 |
|
457 |
|
458 |
|
459 |
|
460 |
|
461 |
|
462 |
|
463 |
|
464 |
|
465 |
|
466 |
|
467 |
|
468 |
|
469 |
|
470 |
|
471 |
|
472 |
|
473 |
|
474 |
|
475 |
|
476 |
|
477 |
|
478 |
|
479 |
|
480 |
|
481 |
|
482 | export type MergeDeep<Destination, Source, Options extends MergeDeepOptions = {}> = MergeDeepWithDefaultOptions<
|
483 | SimplifyDeepExcludeArray<Destination>,
|
484 | SimplifyDeepExcludeArray<Source>,
|
485 | Options
|
486 | >;
|