@depup/type-fest 5.4.4-depup.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (201) hide show
  1. package/README.md +25 -0
  2. package/index.d.ts +211 -0
  3. package/license-cc0 +121 -0
  4. package/license-mit +9 -0
  5. package/package.json +85 -0
  6. package/readme.md +1052 -0
  7. package/source/all-extend.d.ts +120 -0
  8. package/source/all-union-fields.d.ts +91 -0
  9. package/source/and.d.ts +81 -0
  10. package/source/array-element.d.ts +46 -0
  11. package/source/array-indices.d.ts +25 -0
  12. package/source/array-reverse.d.ts +84 -0
  13. package/source/array-slice.d.ts +132 -0
  14. package/source/array-splice.d.ts +104 -0
  15. package/source/array-tail.d.ts +70 -0
  16. package/source/array-values.d.ts +24 -0
  17. package/source/arrayable.d.ts +31 -0
  18. package/source/async-return-type.d.ts +28 -0
  19. package/source/asyncify.d.ts +25 -0
  20. package/source/basic.d.ts +38 -0
  21. package/source/camel-case.d.ts +92 -0
  22. package/source/camel-cased-properties-deep.d.ts +99 -0
  23. package/source/camel-cased-properties.d.ts +45 -0
  24. package/source/characters.d.ts +67 -0
  25. package/source/conditional-except.d.ts +47 -0
  26. package/source/conditional-keys.d.ts +63 -0
  27. package/source/conditional-pick-deep.d.ts +120 -0
  28. package/source/conditional-pick.d.ts +46 -0
  29. package/source/conditional-simplify-deep.d.ts +73 -0
  30. package/source/conditional-simplify.d.ts +50 -0
  31. package/source/delimiter-case.d.ts +77 -0
  32. package/source/delimiter-cased-properties-deep.d.ts +108 -0
  33. package/source/delimiter-cased-properties.d.ts +48 -0
  34. package/source/distributed-omit.d.ts +96 -0
  35. package/source/distributed-pick.d.ts +92 -0
  36. package/source/empty-object.d.ts +51 -0
  37. package/source/entries.d.ts +64 -0
  38. package/source/entry.d.ts +67 -0
  39. package/source/exact.d.ts +73 -0
  40. package/source/except.d.ts +112 -0
  41. package/source/exclude-rest-element.d.ts +40 -0
  42. package/source/exclude-strict.d.ts +51 -0
  43. package/source/exclusify-union.d.ts +147 -0
  44. package/source/extends-strict.d.ts +44 -0
  45. package/source/extract-rest-element.d.ts +30 -0
  46. package/source/extract-strict.d.ts +51 -0
  47. package/source/find-global-type.d.ts +68 -0
  48. package/source/fixed-length-array.d.ts +97 -0
  49. package/source/get.d.ts +227 -0
  50. package/source/global-this.d.ts +24 -0
  51. package/source/globals/index.d.ts +3 -0
  52. package/source/globals/observable-like.d.ts +78 -0
  53. package/source/greater-than-or-equal.d.ts +30 -0
  54. package/source/greater-than.d.ts +58 -0
  55. package/source/has-optional-keys.d.ts +23 -0
  56. package/source/has-readonly-keys.d.ts +23 -0
  57. package/source/has-required-keys.d.ts +61 -0
  58. package/source/has-writable-keys.d.ts +23 -0
  59. package/source/if-any.d.ts +28 -0
  60. package/source/if-empty-object.d.ts +30 -0
  61. package/source/if-never.d.ts +28 -0
  62. package/source/if-null.d.ts +28 -0
  63. package/source/if-unknown.d.ts +28 -0
  64. package/source/if.d.ts +102 -0
  65. package/source/includes.d.ts +24 -0
  66. package/source/int-closed-range.d.ts +47 -0
  67. package/source/int-range.d.ts +69 -0
  68. package/source/internal/array.d.ts +151 -0
  69. package/source/internal/characters.d.ts +65 -0
  70. package/source/internal/enforce-optional.d.ts +49 -0
  71. package/source/internal/index.d.ts +11 -0
  72. package/source/internal/keys.d.ts +100 -0
  73. package/source/internal/numeric.d.ts +151 -0
  74. package/source/internal/object.d.ts +292 -0
  75. package/source/internal/string.d.ts +202 -0
  76. package/source/internal/tuple.d.ts +79 -0
  77. package/source/internal/type.d.ts +164 -0
  78. package/source/invariant-of.d.ts +85 -0
  79. package/source/is-any.d.ts +31 -0
  80. package/source/is-equal.d.ts +42 -0
  81. package/source/is-float.d.ts +43 -0
  82. package/source/is-integer.d.ts +60 -0
  83. package/source/is-literal.d.ts +315 -0
  84. package/source/is-lowercase.d.ts +38 -0
  85. package/source/is-never.d.ts +56 -0
  86. package/source/is-null.d.ts +22 -0
  87. package/source/is-nullable.d.ts +30 -0
  88. package/source/is-optional-key-of.d.ts +51 -0
  89. package/source/is-optional.d.ts +28 -0
  90. package/source/is-readonly-key-of.d.ts +55 -0
  91. package/source/is-required-key-of.d.ts +51 -0
  92. package/source/is-tuple.d.ts +92 -0
  93. package/source/is-undefined.d.ts +22 -0
  94. package/source/is-union.d.ts +40 -0
  95. package/source/is-unknown.d.ts +43 -0
  96. package/source/is-uppercase.d.ts +38 -0
  97. package/source/is-writable-key-of.d.ts +51 -0
  98. package/source/iterable-element.d.ts +66 -0
  99. package/source/join.d.ts +79 -0
  100. package/source/json-value.d.ts +33 -0
  101. package/source/jsonifiable.d.ts +37 -0
  102. package/source/jsonify.d.ts +127 -0
  103. package/source/kebab-case.d.ts +46 -0
  104. package/source/kebab-cased-properties-deep.d.ts +65 -0
  105. package/source/kebab-cased-properties.d.ts +42 -0
  106. package/source/key-as-string.d.ts +27 -0
  107. package/source/keys-of-union.d.ts +44 -0
  108. package/source/last-array-element.d.ts +36 -0
  109. package/source/less-than-or-equal.d.ts +24 -0
  110. package/source/less-than.d.ts +28 -0
  111. package/source/literal-to-primitive-deep.d.ts +71 -0
  112. package/source/literal-to-primitive.d.ts +38 -0
  113. package/source/literal-union.d.ts +39 -0
  114. package/source/merge-deep.d.ts +495 -0
  115. package/source/merge-exclusive.d.ts +45 -0
  116. package/source/merge.d.ts +62 -0
  117. package/source/multidimensional-array.d.ts +38 -0
  118. package/source/multidimensional-readonly-array.d.ts +38 -0
  119. package/source/non-empty-object.d.ts +38 -0
  120. package/source/non-empty-string.d.ts +32 -0
  121. package/source/non-empty-tuple.d.ts +24 -0
  122. package/source/numeric.d.ts +226 -0
  123. package/source/object-merge.d.ts +194 -0
  124. package/source/omit-deep.d.ts +154 -0
  125. package/source/omit-index-signature.d.ts +98 -0
  126. package/source/opaque.d.ts +3 -0
  127. package/source/optional-keys-of.d.ts +46 -0
  128. package/source/or.d.ts +89 -0
  129. package/source/override-properties.d.ts +43 -0
  130. package/source/package-json.d.ts +710 -0
  131. package/source/partial-deep.d.ts +155 -0
  132. package/source/partial-on-undefined-deep.d.ts +81 -0
  133. package/source/pascal-case.d.ts +51 -0
  134. package/source/pascal-cased-properties-deep.d.ts +72 -0
  135. package/source/pascal-cased-properties.d.ts +42 -0
  136. package/source/paths.d.ts +241 -0
  137. package/source/pick-deep.d.ts +152 -0
  138. package/source/pick-index-signature.d.ts +52 -0
  139. package/source/primitive.d.ts +15 -0
  140. package/source/promisable.d.ts +27 -0
  141. package/source/readonly-deep.d.ts +116 -0
  142. package/source/readonly-keys-of.d.ts +38 -0
  143. package/source/readonly-tuple.d.ts +34 -0
  144. package/source/remove-prefix.d.ts +132 -0
  145. package/source/replace.d.ts +87 -0
  146. package/source/require-all-or-none.d.ts +54 -0
  147. package/source/require-at-least-one.d.ts +50 -0
  148. package/source/require-exactly-one.d.ts +48 -0
  149. package/source/require-one-or-none.d.ts +49 -0
  150. package/source/required-deep.d.ts +76 -0
  151. package/source/required-keys-of.d.ts +38 -0
  152. package/source/schema.d.ts +119 -0
  153. package/source/screaming-snake-case.d.ts +30 -0
  154. package/source/set-field-type.d.ts +67 -0
  155. package/source/set-non-nullable-deep.d.ts +85 -0
  156. package/source/set-non-nullable.d.ts +41 -0
  157. package/source/set-optional.d.ts +46 -0
  158. package/source/set-parameter-type.d.ts +125 -0
  159. package/source/set-readonly.d.ts +44 -0
  160. package/source/set-required-deep.d.ts +64 -0
  161. package/source/set-required.d.ts +79 -0
  162. package/source/set-return-type.d.ts +31 -0
  163. package/source/shared-union-fields-deep.d.ts +180 -0
  164. package/source/shared-union-fields.d.ts +79 -0
  165. package/source/simplify-deep.d.ts +117 -0
  166. package/source/simplify.d.ts +61 -0
  167. package/source/single-key-object.d.ts +28 -0
  168. package/source/snake-case.d.ts +47 -0
  169. package/source/snake-cased-properties-deep.d.ts +65 -0
  170. package/source/snake-cased-properties.d.ts +42 -0
  171. package/source/split-on-rest-element.d.ts +106 -0
  172. package/source/split.d.ts +90 -0
  173. package/source/spread.d.ts +82 -0
  174. package/source/string-repeat.d.ts +49 -0
  175. package/source/string-slice.d.ts +39 -0
  176. package/source/stringified.d.ts +25 -0
  177. package/source/structured-cloneable.d.ts +89 -0
  178. package/source/subtract.d.ts +86 -0
  179. package/source/sum.d.ts +81 -0
  180. package/source/tagged-union.d.ts +53 -0
  181. package/source/tagged.d.ts +263 -0
  182. package/source/trim.d.ts +29 -0
  183. package/source/tsconfig-json.d.ts +1324 -0
  184. package/source/tuple-of.d.ts +80 -0
  185. package/source/tuple-to-object.d.ts +47 -0
  186. package/source/tuple-to-union.d.ts +54 -0
  187. package/source/typed-array.d.ts +19 -0
  188. package/source/undefined-on-partial-deep.d.ts +83 -0
  189. package/source/union-to-intersection.d.ts +35 -0
  190. package/source/union-to-tuple.d.ts +58 -0
  191. package/source/unknown-array.d.ts +27 -0
  192. package/source/unknown-map.d.ts +26 -0
  193. package/source/unknown-record.d.ts +33 -0
  194. package/source/unknown-set.d.ts +26 -0
  195. package/source/unwrap-partial.d.ts +33 -0
  196. package/source/value-of.d.ts +24 -0
  197. package/source/words.d.ts +122 -0
  198. package/source/writable-deep.d.ts +84 -0
  199. package/source/writable-keys-of.d.ts +34 -0
  200. package/source/writable.d.ts +72 -0
  201. package/source/xor.d.ts +83 -0
@@ -0,0 +1,38 @@
1
+ import type {HasRequiredKeys} from './has-required-keys.d.ts';
2
+ import type {RequireAtLeastOne} from './require-at-least-one.d.ts';
3
+
4
+ /**
5
+ Represents an object with at least 1 non-optional key.
6
+
7
+ This is useful when you need an object where all keys are optional, but there must be at least 1 key.
8
+
9
+ @example
10
+ ```
11
+ import type {NonEmptyObject} from 'type-fest';
12
+
13
+ type User = {
14
+ name: string;
15
+ surname: string;
16
+ id: number;
17
+ };
18
+
19
+ type UpdateRequest<Entity extends object> = NonEmptyObject<Partial<Entity>>;
20
+
21
+ const update1: UpdateRequest<User> = {
22
+ name: 'Alice',
23
+ surname: 'Acme',
24
+ };
25
+
26
+ // At least 1 key is required, therefore this will report a 2322 error:
27
+ // Type '{}' is not assignable to type 'UpdateRequest<User>'
28
+ // @ts-expect-error
29
+ const update2: UpdateRequest<User> = {};
30
+ ```
31
+
32
+ @see Use `IsEmptyObject` to check whether an object is empty.
33
+
34
+ @category Object
35
+ */
36
+ export type NonEmptyObject<T extends object> = HasRequiredKeys<T> extends true ? T : RequireAtLeastOne<T, keyof T>;
37
+
38
+ export {};
@@ -0,0 +1,32 @@
1
+ /**
2
+ Matches any non-empty string.
3
+
4
+ This is useful when you need a string that is not empty, for example, as a function parameter.
5
+
6
+ NOTE:
7
+ - This returns `never` not just when instantiated with an empty string, but also when an empty string is a subtype of the instantiated type, like `string` or `Uppercase<string>`.
8
+
9
+ @example
10
+ ```
11
+ import type {NonEmptyString} from 'type-fest';
12
+
13
+ declare function foo<T extends string>(string: NonEmptyString<T>): void;
14
+
15
+ foo('a');
16
+ // OK
17
+
18
+ // @ts-expect-error
19
+ foo('');
20
+ // Error: Argument of type '""' is not assignable to parameter of type 'never'.
21
+
22
+ declare const someString: string;
23
+ // @ts-expect-error
24
+ foo(someString);
25
+ // Error: Argument of type 'string' is not assignable to parameter of type 'never'.
26
+ ```
27
+
28
+ @category String
29
+ */
30
+ export type NonEmptyString<T extends string> = '' extends T ? never : T;
31
+
32
+ export {};
@@ -0,0 +1,24 @@
1
+ /**
2
+ Matches any non-empty tuple.
3
+
4
+ @example
5
+ ```
6
+ import type {NonEmptyTuple} from 'type-fest';
7
+
8
+ const sum = (...numbers: NonEmptyTuple<number>) => numbers.reduce((total, value) => total + value, 0);
9
+
10
+ sum(1, 2, 3);
11
+ // Ok
12
+
13
+ // @ts-expect-error
14
+ sum();
15
+ // Error: Expected at least 1 arguments, but got 0.
16
+ ```
17
+
18
+ @see {@link RequireAtLeastOne} for objects
19
+
20
+ @category Array
21
+ */
22
+ export type NonEmptyTuple<T = unknown> = readonly [T, ...T[]];
23
+
24
+ export {};
@@ -0,0 +1,226 @@
1
+ import type {IsFloat} from './is-float.d.ts';
2
+ import type {IsInteger} from './is-integer.d.ts';
3
+
4
+ export type _Numeric = number | bigint;
5
+
6
+ type Zero = 0 | 0n;
7
+
8
+ /**
9
+ Matches the hidden `Infinity` type.
10
+
11
+ Please upvote [this issue](https://github.com/microsoft/TypeScript/issues/32277) if you want to have this type as a built-in in TypeScript.
12
+
13
+ @see {@link NegativeInfinity}
14
+
15
+ @category Numeric
16
+ */
17
+ // See https://github.com/microsoft/TypeScript/issues/31752
18
+ // eslint-disable-next-line no-loss-of-precision
19
+ export type PositiveInfinity = 1e999;
20
+
21
+ /**
22
+ Matches the hidden `-Infinity` type.
23
+
24
+ Please upvote [this issue](https://github.com/microsoft/TypeScript/issues/32277) if you want to have this type as a built-in in TypeScript.
25
+
26
+ @see {@link PositiveInfinity}
27
+
28
+ @category Numeric
29
+ */
30
+ // See https://github.com/microsoft/TypeScript/issues/31752
31
+ // eslint-disable-next-line no-loss-of-precision
32
+ export type NegativeInfinity = -1e999;
33
+
34
+ /**
35
+ A finite `number`.
36
+ You can't pass a `bigint` as they are already guaranteed to be finite.
37
+
38
+ Use-case: Validating and documenting parameters.
39
+
40
+ Note: This can't detect `NaN`, please upvote [this issue](https://github.com/microsoft/TypeScript/issues/28682) if you want to have this type as a built-in in TypeScript.
41
+
42
+ @example
43
+ ```
44
+ import type {Finite} from 'type-fest';
45
+
46
+ declare function setScore<T extends number>(length: Finite<T>): void;
47
+ ```
48
+
49
+ @category Numeric
50
+ */
51
+ export type Finite<T extends number> = T extends PositiveInfinity | NegativeInfinity ? never : T;
52
+
53
+ /**
54
+ A `number` that is an integer.
55
+
56
+ Use-case: Validating and documenting parameters.
57
+
58
+ @example
59
+ ```
60
+ import type {Integer} from 'type-fest';
61
+
62
+ type SomeInteger = Integer<1>;
63
+ //=> 1
64
+
65
+ type IntegerWithDecimal = Integer<1.0>;
66
+ //=> 1
67
+
68
+ type NegativeInteger = Integer<-1>;
69
+ //=> -1
70
+
71
+ type Float = Integer<1.5>;
72
+ //=> never
73
+
74
+ // Supports non-decimal numbers
75
+
76
+ type OctalInteger = Integer<0o10>;
77
+ //=> 8
78
+
79
+ type BinaryInteger = Integer<0b10>;
80
+ //=> 2
81
+
82
+ type HexadecimalInteger = Integer<0x10>;
83
+ //=> 16
84
+ ```
85
+
86
+ @example
87
+ ```
88
+ import type {Integer} from 'type-fest';
89
+
90
+ declare function setYear<T extends number>(length: Integer<T>): void;
91
+ ```
92
+
93
+ @see {@link NegativeInteger}
94
+ @see {@link NonNegativeInteger}
95
+
96
+ @category Numeric
97
+ */
98
+ // `${bigint}` is a type that matches a valid bigint literal without the `n` (ex. 1, 0b1, 0o1, 0x1)
99
+ // Because T is a number and not a string we can effectively use this to filter out any numbers containing decimal points
100
+ export type Integer<T> =
101
+ T extends unknown // To distributive type
102
+ ? IsInteger<T> extends true ? T : never
103
+ : never; // Never happens
104
+
105
+ /**
106
+ A `number` that is not an integer.
107
+
108
+ Use-case: Validating and documenting parameters.
109
+
110
+ It does not accept `Infinity`.
111
+
112
+ @example
113
+ ```
114
+ import type {Float} from 'type-fest';
115
+
116
+ declare function setPercentage<T extends number>(length: Float<T>): void;
117
+ ```
118
+
119
+ @see {@link Integer}
120
+
121
+ @category Numeric
122
+ */
123
+ export type Float<T> =
124
+ T extends unknown // To distributive type
125
+ ? IsFloat<T> extends true ? T : never
126
+ : never; // Never happens
127
+
128
+ /**
129
+ A negative (`-∞ < x < 0`) `number` that is not an integer.
130
+ Equivalent to `Negative<Float<T>>`.
131
+
132
+ Use-case: Validating and documenting parameters.
133
+
134
+ @see {@link Negative}
135
+ @see {@link Float}
136
+
137
+ @category Numeric
138
+ */
139
+ export type NegativeFloat<T extends number> = Negative<Float<T>>;
140
+
141
+ /**
142
+ A negative `number`/`bigint` (`-∞ < x < 0`)
143
+
144
+ Use-case: Validating and documenting parameters.
145
+
146
+ @see {@link NegativeInteger}
147
+ @see {@link NonNegative}
148
+
149
+ @category Numeric
150
+ */
151
+ export type Negative<T extends _Numeric> = T extends Zero ? never : `${T}` extends `-${string}` ? T : never;
152
+
153
+ /**
154
+ A negative (`-∞ < x < 0`) `number` that is an integer.
155
+ Equivalent to `Negative<Integer<T>>`.
156
+
157
+ You can't pass a `bigint` as they are already guaranteed to be integers, instead use `Negative<T>`.
158
+
159
+ Use-case: Validating and documenting parameters.
160
+
161
+ @see {@link Negative}
162
+ @see {@link Integer}
163
+
164
+ @category Numeric
165
+ */
166
+ export type NegativeInteger<T extends number> = Negative<Integer<T>>;
167
+
168
+ /**
169
+ A non-negative `number`/`bigint` (`0 <= x < ∞`).
170
+
171
+ Use-case: Validating and documenting parameters.
172
+
173
+ @see {@link NonNegativeInteger}
174
+ @see {@link Negative}
175
+
176
+ @example
177
+ ```
178
+ import type {NonNegative} from 'type-fest';
179
+
180
+ declare function setLength<T extends number>(length: NonNegative<T>): void;
181
+ ```
182
+
183
+ @category Numeric
184
+ */
185
+ export type NonNegative<T extends _Numeric> = T extends Zero ? T : Negative<T> extends never ? T : never;
186
+
187
+ /**
188
+ A non-negative (`0 <= x < ∞`) `number` that is an integer.
189
+ Equivalent to `NonNegative<Integer<T>>`.
190
+
191
+ You can't pass a `bigint` as they are already guaranteed to be integers, instead use `NonNegative<T>`.
192
+
193
+ Use-case: Validating and documenting parameters.
194
+
195
+ @see {@link NonNegative}
196
+ @see {@link Integer}
197
+
198
+ @example
199
+ ```
200
+ import type {NonNegativeInteger} from 'type-fest';
201
+
202
+ declare function setLength<T extends number>(length: NonNegativeInteger<T>): void;
203
+ ```
204
+
205
+ @category Numeric
206
+ */
207
+ export type NonNegativeInteger<T extends number> = NonNegative<Integer<T>>;
208
+
209
+ /**
210
+ Returns a boolean for whether the given number is a negative number.
211
+
212
+ @see {@link Negative}
213
+
214
+ @example
215
+ ```
216
+ import type {IsNegative} from 'type-fest';
217
+
218
+ type ShouldBeFalse = IsNegative<1>;
219
+ type ShouldBeTrue = IsNegative<-1>;
220
+ ```
221
+
222
+ @category Numeric
223
+ */
224
+ export type IsNegative<T extends _Numeric> = T extends Negative<T> ? true : false;
225
+
226
+ export {};
@@ -0,0 +1,194 @@
1
+ import type {If} from './if.d.ts';
2
+ import type {NormalizedKeys} from './internal/object.d.ts';
3
+ import type {IfNotAnyOrNever, IsExactOptionalPropertyTypesEnabled, MapsSetsOrArrays} from './internal/type.d.ts';
4
+ import type {IsNever} from './is-never.d.ts';
5
+ import type {IsOptionalKeyOf} from './is-optional-key-of.d.ts';
6
+ import type {OmitIndexSignature} from './omit-index-signature.d.ts';
7
+ import type {PickIndexSignature} from './pick-index-signature.d.ts';
8
+ import type {RequiredKeysOf} from './required-keys-of.d.ts';
9
+ import type {Simplify} from './simplify.d.ts';
10
+
11
+ /**
12
+ Merge two object types into a new object type, where keys from the second override keys from the first.
13
+
14
+ @example
15
+ ```ts
16
+ import type {ObjectMerge} from 'type-fest';
17
+
18
+ type PartialOverride = ObjectMerge<{foo: string; bar: string}, {foo: number; baz: number}>;
19
+ //=> {foo: number; baz: number; bar: string}
20
+
21
+ type CompleteOverride = ObjectMerge<{foo: string; bar: number}, {foo: number; bar: string; baz: boolean}>;
22
+ //=> {foo: number; bar: string; baz: boolean}
23
+
24
+ type NoOverride = ObjectMerge<{foo: string; bar: number}, {baz: boolean; qux: bigint}>;
25
+ //=> {baz: boolean; qux: bigint; foo: string; bar: number}
26
+ ```
27
+
28
+ Use-cases:
29
+
30
+ Can be used to accurately type object spread and `Object.assign`. The built-in inference for these operations can sometimes be unsound, especially when index signatures are involved.
31
+
32
+ In the following example, both object spread and `Object.assign` produce a type that allows unsafe usage, whereas `ObjectMerge` produces a type that prevents this unsafe access.
33
+
34
+ @example
35
+ ```ts
36
+ import type {ObjectMerge} from 'type-fest';
37
+
38
+ const left: {a: string} = {a: '1'};
39
+ const right: {[x: string]: number} = {a: 1};
40
+
41
+ const inferred = {...left, ...right};
42
+ //=> {a: string}
43
+
44
+ inferred.a.toUpperCase(); // No compile time error, but fails at runtime.
45
+
46
+ const objectAssign = Object.assign(left, right);
47
+ //=> {a: string} & {[x: string]: number}
48
+
49
+ objectAssign.a.toUpperCase(); // No compile time error, but fails at runtime.
50
+
51
+ declare const objectMerge: ObjectMerge<typeof left, typeof right>;
52
+ //=> {[x: string]: string | number; a: string | number}
53
+
54
+ // @ts-expect-error
55
+ objectMerge.a.toUpperCase(); // Correctly errors at compile time.
56
+ ```
57
+
58
+ Can be used to merge generic type arguments.
59
+
60
+ In the following example, object spread without `ObjectMerge` produces an intersection type that is not particularly usable, whereas `ObjectMerge` produces a correctly merged and usable result.
61
+
62
+ @example
63
+ ```ts
64
+ import type {ObjectMerge} from 'type-fest';
65
+
66
+ function withoutObjectMerge<T extends object, U extends object>(left: T, right: U) {
67
+ return {...left, ...right};
68
+ }
69
+
70
+ const result1 = withoutObjectMerge({a: 1}, {a: 'one'});
71
+ //=> {a: number} & {a: string}
72
+
73
+ const {a} = result1;
74
+ //=> never
75
+
76
+ function withObjectMerge<T extends object, U extends object>(left: T, right: U) {
77
+ return {...left, ...right} as unknown as ObjectMerge<T, U>;
78
+ }
79
+
80
+ const result2 = withObjectMerge({b: 1}, {b: 'one'});
81
+ //=> {b: string}
82
+
83
+ const {b} = result2;
84
+ //=> string
85
+ ```
86
+
87
+ Note: If you want a simple merge where properties from the second object always override properties from the first object without considering runtime implications, refer to the {@link Merge} type.
88
+
89
+ @see {@link Merge}
90
+ @category Object
91
+ */
92
+ export type ObjectMerge<First extends object, Second extends object> =
93
+ IfNotAnyOrNever<First, IfNotAnyOrNever<Second, First extends unknown // For distributing `First`
94
+ ? Second extends unknown // For distributing `Second`
95
+ ? First extends MapsSetsOrArrays
96
+ ? unknown
97
+ : Second extends MapsSetsOrArrays
98
+ ? unknown
99
+ : _ObjectMerge<
100
+ First,
101
+ Second,
102
+ NormalizedLiteralKeys<First>,
103
+ NormalizedLiteralKeys<Second>,
104
+ IsExactOptionalPropertyTypesEnabled extends true ? Required<First> : First,
105
+ IsExactOptionalPropertyTypesEnabled extends true ? Required<Second> : Second
106
+ >
107
+ : never // Should never happen
108
+ : never>, First & Second>; // Should never happen
109
+
110
+ type _ObjectMerge<
111
+ First extends object,
112
+ Second extends object,
113
+ NormalizedFirstLiteralKeys extends PropertyKey,
114
+ NormalizedSecondLiteralKeys extends PropertyKey,
115
+ NormalizedFirst extends object,
116
+ NormalizedSecond extends object,
117
+ > = Simplify<{
118
+ // Map over literal keys of `Second`, except those that are optional and also present in `First`.
119
+ -readonly [P in keyof Second as P extends NormalizedSecondLiteralKeys
120
+ ? P extends NormalizedFirstLiteralKeys
121
+ ? If<IsOptionalKeyOf<Second, P>, never, P>
122
+ : P
123
+ : never]:
124
+ | Second[P]
125
+ | (P extends NormalizedKeys<keyof PickIndexSignature<First>>
126
+ ? If<IsOptionalKeyOf<Second, P>, First[NormalizedKeys<P> & keyof First], never>
127
+ : never)
128
+ } & {
129
+ // Map over literal keys of `First`, except those that are not present in `Second`.
130
+ -readonly [P in keyof First as P extends NormalizedFirstLiteralKeys
131
+ ? P extends NormalizedSecondLiteralKeys
132
+ ? never
133
+ : P
134
+ : never]:
135
+ | First[P]
136
+ // If there's a matching index signature in `Second`, then add the type for it as well,
137
+ // for example, in `ObjectMerge<{a: string}, {[x: string]: number}>`, `a` is of type `string | number`.
138
+ | (P extends NormalizedKeys<keyof Second>
139
+ ? Second[NormalizedKeys<P> & keyof Second]
140
+ : never);
141
+ } & {
142
+ // Map over non-literal keys of `Second`.
143
+ -readonly [P in keyof Second as P extends NormalizedSecondLiteralKeys ? never : P]:
144
+ | Second[P]
145
+ // If there's a matching key in `First`, then add the type for it as well,
146
+ // for example, in `ObjectMerge<{a: number}, {[x: string]: string}>`,
147
+ // the resulting type is `{[x: string]: number | string; a: number | string}`.
148
+ // But, exclude keys from `First` that would surely get overwritten,
149
+ // for example, in `ObjectMerge<{a: number}, {[x: string]: string; a: string}>`,
150
+ // `a` from `First` would get overwritten by `a` from `Second`, so don't add type for it.
151
+ | (NormalizedKeys<P> & Exclude<keyof First, NormalizedKeys<RequiredKeysOf<OmitIndexSignature<Second>>>> extends infer NonOverwrittenKeysOfFirst
152
+ ? If<IsNever<NonOverwrittenKeysOfFirst>, // This check is required because indexing with `never` doesn't always yield `never`, for example, `{[x: string]: number}[never]` results in `number`.
153
+ never,
154
+ NormalizedFirst[NonOverwrittenKeysOfFirst & keyof NormalizedFirst]>
155
+ : never); // Should never happen
156
+ } & {
157
+ // Map over non-literal keys of `First`.
158
+ -readonly [P in keyof First as P extends NormalizedFirstLiteralKeys ? never : P]:
159
+ | First[P]
160
+ | If<IsNever<NormalizedKeys<P> & keyof Second>, // This check is required because indexing with `never` doesn't always yield `never`, for example, `{[x: string]: number}[never]` results in `number`.
161
+ never,
162
+ NormalizedSecond[NormalizedKeys<P> & keyof NormalizedSecond]>;
163
+ } & {
164
+ // Handle optional keys of `Second` that are also present in `First`.
165
+ // Map over `First` instead of `Second` because the modifier is in accordance with `First`.
166
+ -readonly [P in keyof First as P extends NormalizedFirstLiteralKeys
167
+ ? P extends NormalizedSecondLiteralKeys
168
+ ? If<IsOptionalKeyOf<Second, NormalizedKeys<P> & keyof Second>, P, never>
169
+ : never
170
+ : never]:
171
+ | First[P]
172
+ | NormalizedSecond[NormalizedKeys<P> & keyof NormalizedSecond]
173
+ }>;
174
+
175
+ /**
176
+ Get literal keys of a type, including both string and number representations wherever applicable.
177
+
178
+ @example
179
+ ```ts
180
+ type A = NormalizedLiteralKeys<{0: string; '1'?: number; foo: boolean}>;
181
+ //=> 0 | '0' | 1 | '1' | 'foo'
182
+
183
+ type B = NormalizedLiteralKeys<{[x: string]: string | number; 0: string; '1'?: number}>;
184
+ //=> 0 | '0' | 1 | '1'
185
+
186
+ type C = NormalizedLiteralKeys<{[x: string]: unknown}>;
187
+ //=> never
188
+ ```
189
+ */
190
+ type NormalizedLiteralKeys<Type> = Type extends unknown // For distributing `Type`
191
+ ? NormalizedKeys<keyof OmitIndexSignature<Type>>
192
+ : never; // Should never happen
193
+
194
+ export {};
@@ -0,0 +1,154 @@
1
+ import type {ArraySplice} from './array-splice.d.ts';
2
+ import type {ExactKey, IsArrayReadonly, NonRecursiveType, SetArrayAccess, ToString} from './internal/index.d.ts';
3
+ import type {IsEqual} from './is-equal.d.ts';
4
+ import type {IsNever} from './is-never.d.ts';
5
+ import type {LiteralUnion} from './literal-union.d.ts';
6
+ import type {Paths} from './paths.d.ts';
7
+ import type {SimplifyDeep} from './simplify-deep.d.ts';
8
+ import type {Simplify} from './simplify.d.ts';
9
+ import type {UnionToTuple} from './union-to-tuple.d.ts';
10
+ import type {UnknownArray} from './unknown-array.d.ts';
11
+
12
+ /**
13
+ Omit properties from a deeply-nested object.
14
+
15
+ It supports recursing into arrays.
16
+
17
+ It supports removing specific items from an array, replacing each removed item with unknown at the specified index.
18
+
19
+ Use-case: Remove unneeded parts of complex objects.
20
+
21
+ Use [`Omit`](https://www.typescriptlang.org/docs/handbook/utility-types.html#omittype-keys) if you only need one level deep.
22
+
23
+ @example
24
+ ```
25
+ import type {OmitDeep} from 'type-fest';
26
+
27
+ type Info = {
28
+ userInfo: {
29
+ name: string;
30
+ uselessInfo: {
31
+ foo: string;
32
+ };
33
+ };
34
+ };
35
+
36
+ type UsefulInfo = OmitDeep<Info, 'userInfo.uselessInfo'>;
37
+ //=> {userInfo: {name: string}}
38
+
39
+ // Supports removing multiple paths
40
+ type Info1 = {
41
+ userInfo: {
42
+ name: string;
43
+ uselessField: string;
44
+ uselessInfo: {
45
+ foo: string;
46
+ };
47
+ };
48
+ };
49
+
50
+ type UsefulInfo1 = OmitDeep<Info1, 'userInfo.uselessInfo' | 'userInfo.uselessField'>;
51
+ //=> {userInfo: {name: string}}
52
+
53
+ // Supports array
54
+ type A = OmitDeep<[1, 'foo', 2], '1'>;
55
+ //=> [1, unknown, 2]
56
+
57
+ // Supports recursing into array
58
+
59
+ type Info2 = {
60
+ address: [
61
+ {
62
+ street: string;
63
+ },
64
+ {
65
+ street2: string;
66
+ foo: string;
67
+ },
68
+ ];
69
+ };
70
+
71
+ type AddressInfo = OmitDeep<Info2, 'address.1.foo'>;
72
+ //=> {address: [{street: string}, {street2: string}]}
73
+ ```
74
+
75
+ @category Object
76
+ @category Array
77
+ */
78
+ export type OmitDeep<T, PathUnion extends LiteralUnion<Paths<T>, string>> =
79
+ SimplifyDeep<
80
+ OmitDeepHelper<T, UnionToTuple<PathUnion>>,
81
+ UnknownArray>;
82
+
83
+ /**
84
+ Internal helper for {@link OmitDeep}.
85
+
86
+ Recursively transforms `T` by applying {@link OmitDeepWithOnePath} for each path in `PathTuple`.
87
+ */
88
+ type OmitDeepHelper<T, PathTuple extends UnknownArray> =
89
+ PathTuple extends [infer Path, ...infer RestPaths]
90
+ ? OmitDeepHelper<OmitDeepWithOnePath<T, Path & (string | number)>, RestPaths>
91
+ : T;
92
+
93
+ /**
94
+ Omit one path from the given object/array.
95
+ */
96
+ type OmitDeepWithOnePath<T, Path extends string | number> =
97
+ T extends NonRecursiveType
98
+ ? T
99
+ : T extends UnknownArray ? SetArrayAccess<OmitDeepArrayWithOnePath<T, Path>, IsArrayReadonly<T>>
100
+ : T extends object ? OmitDeepObjectWithOnePath<T, Path>
101
+ : T;
102
+
103
+ /**
104
+ Omit one path from the given object.
105
+ */
106
+ type OmitDeepObjectWithOnePath<ObjectT extends object, P extends string | number> =
107
+ P extends `${infer RecordKeyInPath}.${infer SubPath}`
108
+ ? {
109
+ [Key in keyof ObjectT]:
110
+ IsEqual<RecordKeyInPath, ToString<Key>> extends true
111
+ ? ExactKey<ObjectT, Key> extends infer RealKey
112
+ ? RealKey extends keyof ObjectT
113
+ ? OmitDeepWithOnePath<ObjectT[RealKey], SubPath>
114
+ : ObjectT[Key]
115
+ : ObjectT[Key]
116
+ : ObjectT[Key]
117
+ }
118
+ : ExactKey<ObjectT, P> extends infer Key
119
+ ? IsNever<Key> extends true
120
+ ? ObjectT
121
+ : Key extends PropertyKey
122
+ ? Simplify<Omit<ObjectT, Key>> // `Simplify` to prevent `Omit` from appearing in the resulting type
123
+ : ObjectT
124
+ : ObjectT;
125
+
126
+ /**
127
+ Omit one path from from the given array.
128
+
129
+ It replaces the item to `unknown` at the given index.
130
+
131
+ @example
132
+ ```
133
+ type A = OmitDeepArrayWithOnePath<[10, 20, 30, 40], 2>;
134
+ //=> type A = [10, 20, unknown, 40];
135
+ ```
136
+ */
137
+ type OmitDeepArrayWithOnePath<ArrayType extends UnknownArray, P extends string | number> =
138
+ // Handle paths that are `${number}.${string}`
139
+ P extends `${infer ArrayIndex extends number}.${infer SubPath}`
140
+ // If `ArrayIndex` is equal to `number`
141
+ ? number extends ArrayIndex
142
+ ? Array<OmitDeepWithOnePath<NonNullable<ArrayType[number]>, SubPath>>
143
+ // If `ArrayIndex` is a number literal
144
+ : ArraySplice<ArrayType, ArrayIndex, 1, [OmitDeepWithOnePath<NonNullable<ArrayType[ArrayIndex]>, SubPath>]>
145
+ // If the path is equal to `number`
146
+ : P extends `${infer ArrayIndex extends number}`
147
+ // If `ArrayIndex` is `number`
148
+ ? number extends ArrayIndex
149
+ ? []
150
+ // If `ArrayIndex` is a number literal
151
+ : ArraySplice<ArrayType, ArrayIndex, 1, [unknown]>
152
+ : ArrayType;
153
+
154
+ export {};