UNPKG

4.53 kBTypeScriptView Raw
1import { At } from '../Any/At';
2import { Key } from '../Any/Key';
3import { List } from '../List/List';
4import { Depth } from './_Internal';
5import { BuiltIn } from '../Misc/BuiltIn';
6import { _Omit } from './Omit';
7import { Length } from '../List/Length';
8import { Has } from '../Union/Has';
9/**
10 * @hidden
11 */
12declare type Longer<L extends List, L1 extends List> = L extends unknown ? L1 extends unknown ? {
13 0: 0;
14 1: 1;
15}[Has<keyof L, keyof L1>] : never : never;
16/**
17 * @hidden
18 */
19declare type PatchProp<OK, O1K, fill, OKeys extends Key, K extends Key> = K extends OKeys ? OK extends fill ? O1K : OK : O1K;
20/**
21 * @hidden
22 */
23declare type PatchFlatObject<O extends object, O1 extends object, fill, OKeys extends Key = keyof O> = {
24 [K in keyof (O & _Omit<O1, OKeys>)]: PatchProp<At<O, K>, At<O1, K>, fill, OKeys, K>;
25} & {};
26/**
27 * @hidden
28 */
29declare type PatchFlatList<L extends List, L1 extends List, ignore extends object, fill> = number extends Length<L | L1> ? PatchFlatChoice<L[number], L1[number], ignore, fill>[] : Longer<L, L1> extends 1 ? {
30 [K in keyof L]: PatchProp<L[K], At<L1, K>, fill, keyof L, K>;
31} : {
32 [K in keyof L1]: PatchProp<At<L, K>, L1[K], fill, keyof L, K>;
33};
34/**
35 * @hidden
36 */
37export declare type PatchFlatChoice<O extends object, O1 extends object, ignore extends object, fill> = O extends ignore ? O : O1 extends ignore ? O : O extends List ? O1 extends List ? PatchFlatList<O, O1, ignore, fill> : PatchFlatObject<O, O1, fill> : PatchFlatObject<O, O1, fill>;
38/**
39 * @hidden
40 */
41export declare type PatchFlat<O extends object, O1 extends object, ignore extends object = BuiltIn, fill = never> = O extends unknown ? O1 extends unknown ? PatchFlatChoice<O, O1, ignore, fill> : never : never;
42/**
43 * @hidden
44 */
45declare type PatchDeepList<L extends List, L1 extends List, ignore extends object, fill> = number extends Length<L | L1> ? PatchDeepChoice<L[number], L1[number], ignore, fill, never, any>[] : Longer<L, L1> extends 1 ? {
46 [K in keyof L]: PatchDeepChoice<L[K], At<L1, K>, ignore, fill, keyof L, K>;
47} : {
48 [K in keyof L1]: PatchDeepChoice<At<L, K>, L1[K], ignore, fill, keyof L, K>;
49};
50/**
51 * @hidden
52 */
53declare type PatchDeepObject<O extends object, O1 extends object, ignore extends object, fill, OKeys extends Key = keyof O> = {
54 [K in keyof (O & _Omit<O1, OKeys>)]: PatchDeepChoice<At<O, K>, At<O1, K>, ignore, fill, OKeys, K>;
55};
56/**
57 * @hidden
58 */
59declare type PatchDeepChoice<OK, O1K, ignore extends object, fill, OKeys extends Key, K extends Key> = [
60 OK
61] extends [never] ? PatchProp<OK, O1K, fill, OKeys, K> : [
62 O1K
63] extends [never] ? PatchProp<OK, O1K, fill, OKeys, K> : OK extends ignore ? PatchProp<OK, O1K, fill, OKeys, K> : O1K extends ignore ? PatchProp<OK, O1K, fill, OKeys, K> : OK extends List ? O1K extends List ? PatchDeepList<OK, O1K, ignore, fill> : PatchProp<OK, O1K, fill, OKeys, K> : OK extends object ? O1K extends object ? PatchDeepObject<OK, O1K, ignore, fill> : PatchProp<OK, O1K, fill, OKeys, K> : PatchProp<OK, O1K, fill, OKeys, K>;
64/**
65 * @hidden
66 */
67export declare type PatchDeep<O extends object, O1 extends object, ignore extends object, fill> = O extends unknown ? O1 extends unknown ? PatchDeepChoice<O, O1, ignore, fill, 'x', 'y'> : never : never;
68/**
69 * Complete the fields of `O` with the ones of `O1`. This is a version of
70 * [[Merge]] that does NOT handle optional fields, it only completes fields of
71 * `O` with the ones of `O1`.
72 * @param O to complete
73 * @param O1 to copy from
74 * @param depth (?=`'flat'`) 'deep' to do it deeply
75 * @param ignore (?=`BuiltIn`) types not to merge
76 * @param fill (?=`never`) types of `O` to be replaced with ones of `O1`
77 * @returns [[Object]]
78 * @example
79 * ```ts
80 * import {O} from 'ts-toolbelt'
81 *
82 * type O = {
83 * name?: string
84 * age? : number
85 * zip? : string
86 * pay : {
87 * cvv?: number
88 * }
89 * }
90 *
91 * type O1 = {
92 * age : number
93 * zip?: number
94 * city: string
95 * pay : {
96 * cvv : number
97 * ccn?: string
98 * }
99 * }
100 *
101 * type test = O.Patch<O, O1, 'deep'>
102 * // {
103 * // name?: string;
104 * // age?: number;
105 * // zip?: string | number;
106 * // pay: {
107 * // cvv?: number;
108 * // ccn?: string;
109 * // };
110 * // city: string;
111 * // }
112 * ```
113 */
114export declare type Patch<O extends object, O1 extends object, depth extends Depth = 'flat', ignore extends object = BuiltIn, fill extends any = never> = {
115 'flat': PatchFlat<O, O1, ignore, fill>;
116 'deep': PatchDeep<O, O1, ignore, fill>;
117}[depth];
118export {};