@tesouro/embedded-components-loader 0.11.248 → 0.11.250

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.
@@ -15,13 +15,84 @@ type Primitive =
15
15
  | symbol
16
16
  | bigint;
17
17
 
18
- declare global {
19
- // eslint-disable-next-line @typescript-eslint/consistent-type-definitions -- It has to be an `interface` so that it can be merged.
20
- interface SymbolConstructor {
21
- readonly observable: symbol;
22
- }
18
+ /**
19
+ Returns a boolean for whether the given type is `any`.
20
+
21
+ @link https://stackoverflow.com/a/49928360/1490091
22
+
23
+ Useful in type utilities, such as disallowing `any`s to be passed to a function.
24
+
25
+ @example
26
+ ```
27
+ import type {IsAny} from 'type-fest';
28
+
29
+ const typedObject = {a: 1, b: 2} as const;
30
+ const anyObject: any = {a: 1, b: 2};
31
+
32
+ function get<O extends (IsAny<O> extends true ? {} : Record<string, number>), K extends keyof O = keyof O>(object: O, key: K) {
33
+ return object[key];
23
34
  }
24
35
 
36
+ const typedA = get(typedObject, 'a');
37
+ //=> 1
38
+
39
+ const anyA = get(anyObject, 'a');
40
+ //=> any
41
+ ```
42
+
43
+ @category Type Guard
44
+ @category Utilities
45
+ */
46
+ type IsAny<T> = 0 extends 1 & NoInfer<T> ? true : false;
47
+
48
+ /**
49
+ Returns a boolean for whether the given key is an optional key of type.
50
+
51
+ This is useful when writing utility types or schema validators that need to differentiate `optional` keys.
52
+
53
+ @example
54
+ ```
55
+ import type {IsOptionalKeyOf} from 'type-fest';
56
+
57
+ type User = {
58
+ name: string;
59
+ surname: string;
60
+
61
+ luckyNumber?: number;
62
+ };
63
+
64
+ type Admin = {
65
+ name: string;
66
+ surname?: string;
67
+ };
68
+
69
+ type T1 = IsOptionalKeyOf<User, 'luckyNumber'>;
70
+ //=> true
71
+
72
+ type T2 = IsOptionalKeyOf<User, 'name'>;
73
+ //=> false
74
+
75
+ type T3 = IsOptionalKeyOf<User, 'name' | 'luckyNumber'>;
76
+ //=> boolean
77
+
78
+ type T4 = IsOptionalKeyOf<User | Admin, 'name'>;
79
+ //=> false
80
+
81
+ type T5 = IsOptionalKeyOf<User | Admin, 'surname'>;
82
+ //=> boolean
83
+ ```
84
+
85
+ @category Type Guard
86
+ @category Utilities
87
+ */
88
+ type IsOptionalKeyOf<Type extends object, Key extends keyof Type> =
89
+ IsAny<Type | Key> extends true ? never
90
+ : Key extends keyof Type
91
+ ? Type extends Record<Key, Type[Key]>
92
+ ? false
93
+ : true
94
+ : false;
95
+
25
96
  /**
26
97
  Extract all optional keys from the given type.
27
98
 
@@ -31,12 +102,12 @@ This is useful when you want to create a new type that contains different type v
31
102
  ```
32
103
  import type {OptionalKeysOf, Except} from 'type-fest';
33
104
 
34
- interface User {
105
+ type User = {
35
106
  name: string;
36
107
  surname: string;
37
108
 
38
109
  luckyNumber?: number;
39
- }
110
+ };
40
111
 
41
112
  const REMOVE_FIELD = Symbol('remove field symbol');
42
113
  type UpdateOperation<Entity extends object> = Except<Partial<Entity>, OptionalKeysOf<Entity>> & {
@@ -44,22 +115,25 @@ type UpdateOperation<Entity extends object> = Except<Partial<Entity>, OptionalKe
44
115
  };
45
116
 
46
117
  const update1: UpdateOperation<User> = {
47
- name: 'Alice'
118
+ name: 'Alice',
48
119
  };
49
120
 
50
121
  const update2: UpdateOperation<User> = {
51
122
  name: 'Bob',
52
- luckyNumber: REMOVE_FIELD
123
+ luckyNumber: REMOVE_FIELD,
53
124
  };
54
125
  ```
55
126
 
56
127
  @category Utilities
57
128
  */
58
- type OptionalKeysOf<BaseType extends object> =
59
- BaseType extends unknown // For distributing `BaseType`
60
- ? (keyof {
61
- [Key in keyof BaseType as BaseType extends Record<Key, BaseType[Key]> ? never : Key]: never
62
- }) & (keyof BaseType) // Intersect with `keyof BaseType` to ensure result of `OptionalKeysOf<BaseType>` is always assignable to `keyof BaseType`
129
+ type OptionalKeysOf<Type extends object> =
130
+ Type extends unknown // For distributing `Type`
131
+ ? (keyof {[Key in keyof Type as
132
+ IsOptionalKeyOf<Type, Key> extends false
133
+ ? never
134
+ : Key
135
+ ]: never
136
+ }) & keyof Type // Intersect with `keyof Type` to ensure result of `OptionalKeysOf<Type>` is always assignable to `keyof Type`
63
137
  : never; // Should never happen
64
138
 
65
139
  /**
@@ -71,24 +145,30 @@ This is useful when you want to create a new type that contains different type v
71
145
  ```
72
146
  import type {RequiredKeysOf} from 'type-fest';
73
147
 
74
- declare function createValidation<Entity extends object, Key extends RequiredKeysOf<Entity> = RequiredKeysOf<Entity>>(field: Key, validator: (value: Entity[Key]) => boolean): ValidatorFn;
148
+ declare function createValidation<
149
+ Entity extends object,
150
+ Key extends RequiredKeysOf<Entity> = RequiredKeysOf<Entity>,
151
+ >(field: Key, validator: (value: Entity[Key]) => boolean): (entity: Entity) => boolean;
75
152
 
76
- interface User {
153
+ type User = {
77
154
  name: string;
78
155
  surname: string;
79
-
80
156
  luckyNumber?: number;
81
- }
157
+ };
82
158
 
83
159
  const validator1 = createValidation<User>('name', value => value.length < 25);
84
160
  const validator2 = createValidation<User>('surname', value => value.length < 25);
161
+
162
+ // @ts-expect-error
163
+ const validator3 = createValidation<User>('luckyNumber', value => value > 0);
164
+ // Error: Argument of type '"luckyNumber"' is not assignable to parameter of type '"name" | "surname"'.
85
165
  ```
86
166
 
87
167
  @category Utilities
88
168
  */
89
- type RequiredKeysOf<BaseType extends object> =
90
- BaseType extends unknown // For distributing `BaseType`
91
- ? Exclude<keyof BaseType, OptionalKeysOf<BaseType>>
169
+ type RequiredKeysOf<Type extends object> =
170
+ Type extends unknown // For distributing `Type`
171
+ ? Exclude<keyof Type, OptionalKeysOf<Type>>
92
172
  : never; // Should never happen
93
173
 
94
174
  /**
@@ -104,29 +184,41 @@ Useful in type utilities, such as checking if something does not occur.
104
184
  ```
105
185
  import type {IsNever, And} from 'type-fest';
106
186
 
107
- // https://github.com/andnp/SimplyTyped/blob/master/src/types/strings.ts
108
- type AreStringsEqual<A extends string, B extends string> =
109
- And<
110
- IsNever<Exclude<A, B>> extends true ? true : false,
111
- IsNever<Exclude<B, A>> extends true ? true : false
112
- >;
187
+ type A = IsNever<never>;
188
+ //=> true
113
189
 
114
- type EndIfEqual<I extends string, O extends string> =
115
- AreStringsEqual<I, O> extends true
116
- ? never
117
- : void;
190
+ type B = IsNever<any>;
191
+ //=> false
118
192
 
119
- function endIfEqual<I extends string, O extends string>(input: I, output: O): EndIfEqual<I, O> {
120
- if (input === output) {
121
- process.exit(0);
122
- }
123
- }
193
+ type C = IsNever<unknown>;
194
+ //=> false
195
+
196
+ type D = IsNever<never[]>;
197
+ //=> false
198
+
199
+ type E = IsNever<object>;
200
+ //=> false
201
+
202
+ type F = IsNever<string>;
203
+ //=> false
204
+ ```
205
+
206
+ @example
207
+ ```
208
+ import type {IsNever} from 'type-fest';
209
+
210
+ type IsTrue<T> = T extends true ? true : false;
124
211
 
125
- endIfEqual('abc', 'abc');
212
+ // When a distributive conditional is instantiated with `never`, the entire conditional results in `never`.
213
+ type A = IsTrue<never>;
126
214
  //=> never
127
215
 
128
- endIfEqual('abc', '123');
129
- //=> void
216
+ // If you don't want that behaviour, you can explicitly add an `IsNever` check before the distributive conditional.
217
+ type IsTrueFixed<T> =
218
+ IsNever<T> extends true ? false : T extends true ? true : false;
219
+
220
+ type B = IsTrueFixed<never>;
221
+ //=> false
130
222
  ```
131
223
 
132
224
  @category Type Guard
@@ -135,61 +227,125 @@ endIfEqual('abc', '123');
135
227
  type IsNever<T> = [T] extends [never] ? true : false;
136
228
 
137
229
  /**
138
- An if-else-like type that resolves depending on whether the given type is `never`.
230
+ An if-else-like type that resolves depending on whether the given `boolean` type is `true` or `false`.
231
+
232
+ Use-cases:
233
+ - You can use this in combination with `Is*` types to create an if-else-like experience. For example, `If<IsAny<any>, 'is any', 'not any'>`.
139
234
 
140
- @see {@link IsNever}
235
+ Note:
236
+ - Returns a union of if branch and else branch if the given type is `boolean` or `any`. For example, `If<boolean, 'Y', 'N'>` will return `'Y' | 'N'`.
237
+ - Returns the else branch if the given type is `never`. For example, `If<never, 'Y', 'N'>` will return `'N'`.
141
238
 
142
239
  @example
143
240
  ```
144
- import type {IfNever} from 'type-fest';
241
+ import type {If} from 'type-fest';
145
242
 
146
- type ShouldBeTrue = IfNever<never>;
147
- //=> true
243
+ type A = If<true, 'yes', 'no'>;
244
+ //=> 'yes'
148
245
 
149
- type ShouldBeBar = IfNever<'not never', 'foo', 'bar'>;
150
- //=> 'bar'
151
- ```
246
+ type B = If<false, 'yes', 'no'>;
247
+ //=> 'no'
152
248
 
153
- @category Type Guard
154
- @category Utilities
155
- */
156
- type IfNever<T, TypeIfNever = true, TypeIfNotNever = false> = (
157
- IsNever<T> extends true ? TypeIfNever : TypeIfNotNever
158
- );
249
+ type C = If<boolean, 'yes', 'no'>;
250
+ //=> 'yes' | 'no'
159
251
 
160
- // Can eventually be replaced with the built-in once this library supports
161
- // TS5.4+ only. Tracked in https://github.com/sindresorhus/type-fest/issues/848
162
- type NoInfer<T> = T extends infer U ? U : never;
252
+ type D = If<any, 'yes', 'no'>;
253
+ //=> 'yes' | 'no'
163
254
 
164
- /**
165
- Returns a boolean for whether the given type is `any`.
255
+ type E = If<never, 'yes', 'no'>;
256
+ //=> 'no'
257
+ ```
166
258
 
167
- @link https://stackoverflow.com/a/49928360/1490091
259
+ @example
260
+ ```
261
+ import type {If, IsAny, IsNever} from 'type-fest';
168
262
 
169
- Useful in type utilities, such as disallowing `any`s to be passed to a function.
263
+ type A = If<IsAny<unknown>, 'is any', 'not any'>;
264
+ //=> 'not any'
265
+
266
+ type B = If<IsNever<never>, 'is never', 'not never'>;
267
+ //=> 'is never'
268
+ ```
170
269
 
171
270
  @example
172
271
  ```
173
- import type {IsAny} from 'type-fest';
272
+ import type {If, IsEqual} from 'type-fest';
174
273
 
175
- const typedObject = {a: 1, b: 2} as const;
176
- const anyObject: any = {a: 1, b: 2};
274
+ type IfEqual<T, U, IfBranch, ElseBranch> = If<IsEqual<T, U>, IfBranch, ElseBranch>;
177
275
 
178
- function get<O extends (IsAny<O> extends true ? {} : Record<string, number>), K extends keyof O = keyof O>(obj: O, key: K) {
179
- return obj[key];
180
- }
276
+ type A = IfEqual<string, string, 'equal', 'not equal'>;
277
+ //=> 'equal'
181
278
 
182
- const typedA = get(typedObject, 'a');
183
- //=> 1
279
+ type B = IfEqual<string, number, 'equal', 'not equal'>;
280
+ //=> 'not equal'
281
+ ```
184
282
 
185
- const anyA = get(anyObject, 'a');
186
- //=> any
283
+ Note: Sometimes using the `If` type can make an implementation non–tail-recursive, which can impact performance. In such cases, it’s better to use a conditional directly. Refer to the following example:
284
+
285
+ @example
286
+ ```
287
+ import type {If, IsEqual, StringRepeat} from 'type-fest';
288
+
289
+ type HundredZeroes = StringRepeat<'0', 100>;
290
+
291
+ // The following implementation is not tail recursive
292
+ type Includes<S extends string, Char extends string> =
293
+ S extends `${infer First}${infer Rest}`
294
+ ? If<IsEqual<First, Char>,
295
+ 'found',
296
+ Includes<Rest, Char>>
297
+ : 'not found';
298
+
299
+ // Hence, instantiations with long strings will fail
300
+ // @ts-expect-error
301
+ type Fails = Includes<HundredZeroes, '1'>;
302
+ // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
303
+ // Error: Type instantiation is excessively deep and possibly infinite.
304
+
305
+ // However, if we use a simple conditional instead of `If`, the implementation becomes tail-recursive
306
+ type IncludesWithoutIf<S extends string, Char extends string> =
307
+ S extends `${infer First}${infer Rest}`
308
+ ? IsEqual<First, Char> extends true
309
+ ? 'found'
310
+ : IncludesWithoutIf<Rest, Char>
311
+ : 'not found';
312
+
313
+ // Now, instantiations with long strings will work
314
+ type Works = IncludesWithoutIf<HundredZeroes, '1'>;
315
+ //=> 'not found'
187
316
  ```
188
317
 
189
318
  @category Type Guard
190
319
  @category Utilities
191
320
  */
192
- type IsAny<T> = 0 extends 1 & NoInfer<T> ? true : false;
321
+ type If<Type extends boolean, IfBranch, ElseBranch> =
322
+ IsNever<Type> extends true
323
+ ? ElseBranch
324
+ : Type extends true
325
+ ? IfBranch
326
+ : ElseBranch;
327
+
328
+ /**
329
+ Matches any primitive, `void`, `Date`, or `RegExp` value.
330
+ */
331
+ type BuiltIns = Primitive | void | Date | RegExp;
332
+
333
+ /**
334
+ Test if the given function has multiple call signatures.
335
+
336
+ Needed to handle the case of a single call signature with properties.
337
+
338
+ Multiple call signatures cannot currently be supported due to a TypeScript limitation.
339
+ @see https://github.com/microsoft/TypeScript/issues/29732
340
+ */
341
+ type HasMultipleCallSignatures<T extends (...arguments_: any[]) => unknown> =
342
+ T extends {(...arguments_: infer A): unknown; (...arguments_: infer B): unknown}
343
+ ? B extends A
344
+ ? A extends B
345
+ ? false
346
+ : true
347
+ : true
348
+ : false;
193
349
 
194
350
  /**
195
351
  Useful to flatten the type output to improve type hints shown in editors. And also to transform an interface into a type to aide with assignability.
@@ -236,20 +392,61 @@ const literal = {foo: 123, bar: 'hello', baz: 456};
236
392
  const someType: SomeType = literal;
237
393
  const someInterface: SomeInterface = literal;
238
394
 
239
- function fn(object: Record<string, unknown>): void {}
395
+ declare function fn(object: Record<string, unknown>): void;
240
396
 
241
397
  fn(literal); // Good: literal object type is sealed
242
398
  fn(someType); // Good: type is sealed
399
+ // @ts-expect-error
243
400
  fn(someInterface); // Error: Index signature for type 'string' is missing in type 'someInterface'. Because `interface` can be re-opened
244
401
  fn(someInterface as Simplify<SomeInterface>); // Good: transform an `interface` into a `type`
245
402
  ```
246
403
 
247
404
  @link https://github.com/microsoft/TypeScript/issues/15300
248
- @see SimplifyDeep
405
+ @see {@link SimplifyDeep}
249
406
  @category Object
250
407
  */
251
408
  type Simplify<T> = {[KeyType in keyof T]: T[KeyType]} & {};
252
409
 
410
+ /**
411
+ Returns a boolean for whether the two given types are equal.
412
+
413
+ @link https://github.com/microsoft/TypeScript/issues/27024#issuecomment-421529650
414
+ @link https://stackoverflow.com/questions/68961864/how-does-the-equals-work-in-typescript/68963796#68963796
415
+
416
+ Use-cases:
417
+ - If you want to make a conditional branch based on the result of a comparison of two types.
418
+
419
+ @example
420
+ ```
421
+ import type {IsEqual} from 'type-fest';
422
+
423
+ // This type returns a boolean for whether the given array includes the given item.
424
+ // `IsEqual` is used to compare the given array at position 0 and the given item and then return true if they are equal.
425
+ type Includes<Value extends readonly any[], Item> =
426
+ Value extends readonly [Value[0], ...infer rest]
427
+ ? IsEqual<Value[0], Item> extends true
428
+ ? true
429
+ : Includes<rest, Item>
430
+ : false;
431
+ ```
432
+
433
+ @category Type Guard
434
+ @category Utilities
435
+ */
436
+ type IsEqual<A, B> =
437
+ [A] extends [B]
438
+ ? [B] extends [A]
439
+ ? _IsEqual<A, B>
440
+ : false
441
+ : false;
442
+
443
+ // This version fails the `equalWrappedTupleIntersectionToBeNeverAndNeverExpanded` test in `test-d/is-equal.ts`.
444
+ type _IsEqual<A, B> =
445
+ (<G>() => G extends A & G | G ? 1 : 2) extends
446
+ (<G>() => G extends B & G | G ? 1 : 2)
447
+ ? true
448
+ : false;
449
+
253
450
  /**
254
451
  Omit any index signatures from the given object type, leaving only explicitly defined properties.
255
452
 
@@ -267,8 +464,9 @@ It relies on the fact that an empty object (`{}`) is assignable to an object wit
267
464
  ```
268
465
  const indexed: Record<string, unknown> = {}; // Allowed
269
466
 
467
+ // @ts-expect-error
270
468
  const keyed: Record<'foo', unknown> = {}; // Error
271
- // => TS2739: Type '{}' is missing the following properties from type 'Record<"foo" | "bar", unknown>': foo, bar
469
+ // TS2739: Type '{}' is missing the following properties from type 'Record<"foo" | "bar", unknown>': foo, bar
272
470
  ```
273
471
 
274
472
  Instead of causing a type error like the above, you can also use a [conditional type](https://www.typescriptlang.org/docs/handbook/2/conditional-types.html) to test whether a type is assignable to another:
@@ -277,19 +475,21 @@ Instead of causing a type error like the above, you can also use a [conditional
277
475
  type Indexed = {} extends Record<string, unknown>
278
476
  ? '✅ `{}` is assignable to `Record<string, unknown>`'
279
477
  : '❌ `{}` is NOT assignable to `Record<string, unknown>`';
280
- // => '✅ `{}` is assignable to `Record<string, unknown>`'
478
+
479
+ type IndexedResult = Indexed;
480
+ //=> '✅ `{}` is assignable to `Record<string, unknown>`'
281
481
 
282
482
  type Keyed = {} extends Record<'foo' | 'bar', unknown>
283
- ? "✅ `{}` is assignable to `Record<'foo' | 'bar', unknown>`"
284
- : "❌ `{}` is NOT assignable to `Record<'foo' | 'bar', unknown>`";
285
- // => "❌ `{}` is NOT assignable to `Record<'foo' | 'bar', unknown>`"
483
+ ? '✅ `{}` is assignable to `Record<\'foo\' | \'bar\', unknown>`'
484
+ : '❌ `{}` is NOT assignable to `Record<\'foo\' | \'bar\', unknown>`';
485
+
486
+ type KeyedResult = Keyed;
487
+ //=> '❌ `{}` is NOT assignable to `Record<\'foo\' | \'bar\', unknown>`'
286
488
  ```
287
489
 
288
490
  Using a [mapped type](https://www.typescriptlang.org/docs/handbook/2/mapped-types.html#further-exploration), you can then check for each `KeyType` of `ObjectType`...
289
491
 
290
492
  ```
291
- import type {OmitIndexSignature} from 'type-fest';
292
-
293
493
  type OmitIndexSignature<ObjectType> = {
294
494
  [KeyType in keyof ObjectType // Map each key of `ObjectType`...
295
495
  ]: ObjectType[KeyType]; // ...to its original value, i.e. `OmitIndexSignature<Foo> == Foo`.
@@ -299,14 +499,12 @@ type OmitIndexSignature<ObjectType> = {
299
499
  ...whether an empty object (`{}`) would be assignable to an object with that `KeyType` (`Record<KeyType, unknown>`)...
300
500
 
301
501
  ```
302
- import type {OmitIndexSignature} from 'type-fest';
303
-
304
502
  type OmitIndexSignature<ObjectType> = {
305
503
  [KeyType in keyof ObjectType
306
- // Is `{}` assignable to `Record<KeyType, unknown>`?
307
- as {} extends Record<KeyType, unknown>
308
- ? ... // ✅ `{}` is assignable to `Record<KeyType, unknown>`
309
- : ... // ❌ `{}` is NOT assignable to `Record<KeyType, unknown>`
504
+ // Is `{}` assignable to `Record<KeyType, unknown>`?
505
+ as {} extends Record<KeyType, unknown>
506
+ ? never // ✅ `{}` is assignable to `Record<KeyType, unknown>`
507
+ : KeyType // ❌ `{}` is NOT assignable to `Record<KeyType, unknown>`
310
508
  ]: ObjectType[KeyType];
311
509
  };
312
510
  ```
@@ -317,27 +515,27 @@ If `{}` is assignable, it means that `KeyType` is an index signature and we want
317
515
  ```
318
516
  import type {OmitIndexSignature} from 'type-fest';
319
517
 
320
- interface Example {
518
+ type Example = {
321
519
  // These index signatures will be removed.
322
- [x: string]: any
323
- [x: number]: any
324
- [x: symbol]: any
325
- [x: `head-${string}`]: string
326
- [x: `${string}-tail`]: string
327
- [x: `head-${string}-tail`]: string
328
- [x: `${bigint}`]: string
329
- [x: `embedded-${number}`]: string
520
+ [x: string]: any;
521
+ [x: number]: any;
522
+ [x: symbol]: any;
523
+ [x: `head-${string}`]: string;
524
+ [x: `${string}-tail`]: string;
525
+ [x: `head-${string}-tail`]: string;
526
+ [x: `${bigint}`]: string;
527
+ [x: `embedded-${number}`]: string;
330
528
 
331
529
  // These explicitly defined keys will remain.
332
530
  foo: 'bar';
333
531
  qux?: 'baz';
334
- }
532
+ };
335
533
 
336
534
  type ExampleWithoutIndexSignatures = OmitIndexSignature<Example>;
337
- // => { foo: 'bar'; qux?: 'baz' | undefined; }
535
+ //=> {foo: 'bar'; qux?: 'baz'}
338
536
  ```
339
537
 
340
- @see PickIndexSignature
538
+ @see {@link PickIndexSignature}
341
539
  @category Object
342
540
  */
343
541
  type OmitIndexSignature<ObjectType> = {
@@ -388,7 +586,7 @@ type ExampleIndexSignature = PickIndexSignature<Example>;
388
586
  // }
389
587
  ```
390
588
 
391
- @see OmitIndexSignature
589
+ @see {@link OmitIndexSignature}
392
590
  @category Object
393
591
  */
394
592
  type PickIndexSignature<ObjectType> = {
@@ -398,23 +596,48 @@ type PickIndexSignature<ObjectType> = {
398
596
  };
399
597
 
400
598
  // Merges two objects without worrying about index signatures.
401
- type SimpleMerge<Destination, Source> = {
599
+ type SimpleMerge<Destination, Source> = Simplify<{
402
600
  [Key in keyof Destination as Key extends keyof Source ? never : Key]: Destination[Key];
403
- } & Source;
601
+ } & Source>;
404
602
 
405
603
  /**
406
604
  Merge two types into a new type. Keys of the second type overrides keys of the first type.
407
605
 
606
+ This is different from the TypeScript `&` (intersection) operator. With `&`, conflicting property types are intersected, which often results in `never`. For example, `{a: string} & {a: number}` makes `a` become `string & number`, which resolves to `never`. With `Merge`, the second type's keys cleanly override the first, so `Merge<{a: string}, {a: number}>` gives `{a: number}` as expected. `Merge` also produces a flattened type (via `Simplify`), making it more readable in IDE tooltips compared to `A & B`.
607
+
408
608
  @example
409
609
  ```
410
610
  import type {Merge} from 'type-fest';
411
611
 
412
- interface Foo {
612
+ type Foo = {
613
+ a: string;
614
+ b: number;
615
+ };
616
+
617
+ type Bar = {
618
+ a: number; // Conflicts with Foo['a']
619
+ c: boolean;
620
+ };
621
+
622
+ // With `&`, `a` becomes `string & number` which is `never`. Not what you want.
623
+ type WithIntersection = (Foo & Bar)['a'];
624
+ //=> never
625
+
626
+ // With `Merge`, `a` is cleanly overridden to `number`.
627
+ type WithMerge = Merge<Foo, Bar>['a'];
628
+ //=> number
629
+ ```
630
+
631
+ @example
632
+ ```
633
+ import type {Merge} from 'type-fest';
634
+
635
+ type Foo = {
413
636
  [x: string]: unknown;
414
637
  [x: number]: unknown;
415
638
  foo: string;
416
639
  bar: symbol;
417
- }
640
+ };
418
641
 
419
642
  type Bar = {
420
643
  [x: number]: number;
@@ -424,7 +647,7 @@ type Bar = {
424
647
  };
425
648
 
426
649
  export type FooBar = Merge<Foo, Bar>;
427
- // => {
650
+ //=> {
428
651
  // [x: string]: unknown;
429
652
  // [x: number]: number;
430
653
  // [x: symbol]: unknown;
@@ -434,41 +657,23 @@ export type FooBar = Merge<Foo, Bar>;
434
657
  // }
435
658
  ```
436
659
 
660
+ Note: If you want a merge type that more accurately reflects the runtime behavior of object spread or `Object.assign`, refer to the {@link ObjectMerge} type.
661
+
662
+ @see {@link ObjectMerge}
437
663
  @category Object
438
664
  */
439
665
  type Merge<Destination, Source> =
440
- Simplify<
441
- SimpleMerge<PickIndexSignature<Destination>, PickIndexSignature<Source>>
442
- & SimpleMerge<OmitIndexSignature<Destination>, OmitIndexSignature<Source>>
443
- >;
444
-
445
- /**
446
- An if-else-like type that resolves depending on whether the given type is `any`.
447
-
448
- @see {@link IsAny}
449
-
450
- @example
451
- ```
452
- import type {IfAny} from 'type-fest';
453
-
454
- type ShouldBeTrue = IfAny<any>;
455
- //=> true
456
-
457
- type ShouldBeBar = IfAny<'not any', 'foo', 'bar'>;
458
- //=> 'bar'
459
- ```
460
-
461
- @category Type Guard
462
- @category Utilities
463
- */
464
- type IfAny<T, TypeIfAny = true, TypeIfNotAny = false> = (
465
- IsAny<T> extends true ? TypeIfAny : TypeIfNotAny
466
- );
666
+ Destination extends unknown // For distributing `Destination`
667
+ ? Source extends unknown // For distributing `Source`
668
+ ? If<IsEqual<Destination, Source>, Destination, _Merge<Destination, Source>>
669
+ : never // Should never happen
670
+ : never; // Should never happen
467
671
 
468
- /**
469
- Matches any primitive, `void`, `Date`, or `RegExp` value.
470
- */
471
- type BuiltIns = Primitive | void | Date | RegExp;
672
+ type _Merge<Destination, Source> =
673
+ Simplify<
674
+ SimpleMerge<PickIndexSignature<Destination>, PickIndexSignature<Source>>
675
+ & SimpleMerge<OmitIndexSignature<Destination>, OmitIndexSignature<Source>>
676
+ >;
472
677
 
473
678
  /**
474
679
  Merges user specified options with default options.
@@ -527,18 +732,22 @@ type ApplyDefaultOptions<
527
732
  Defaults extends Simplify<Omit<Required<Options>, RequiredKeysOf<Options>> & Partial<Record<RequiredKeysOf<Options>, never>>>,
528
733
  SpecifiedOptions extends Options,
529
734
  > =
530
- IfAny<SpecifiedOptions, Defaults,
531
- IfNever<SpecifiedOptions, Defaults,
532
- Simplify<Merge<Defaults, {
533
- [Key in keyof SpecifiedOptions
534
- as Key extends OptionalKeysOf<Options>
535
- ? Extract<SpecifiedOptions[Key], undefined> extends never
536
- ? Key
537
- : never
538
- : Key
539
- ]: SpecifiedOptions[Key]
540
- }> & Required<Options>> // `& Required<Options>` ensures that `ApplyDefaultOptions<SomeOption, ...>` is always assignable to `Required<SomeOption>`
541
- >>;
735
+ _ApplyDefaultOptions<Options, Defaults, SpecifiedOptions> extends infer Result extends Required<Options> // `extends Required<Options>` ensures that `ApplyDefaultOptions<SomeOption, ...>` is always assignable to `Required<SomeOption>`
736
+ ? Result
737
+ : never;
738
+
739
+ type _ApplyDefaultOptions<
740
+ Options,
741
+ Defaults,
742
+ SpecifiedOptions,
743
+ > =
744
+ If<IsAny<SpecifiedOptions>, Defaults,
745
+ If<IsNever<SpecifiedOptions>, Defaults,
746
+ Merge<Defaults, {
747
+ [Key in keyof SpecifiedOptions
748
+ as undefined extends Required<Options>[Key & keyof Options] ? Key : undefined extends SpecifiedOptions[Key] ? never : Key
749
+ ]: SpecifiedOptions[Key]
750
+ }>>>;
542
751
 
543
752
  /**
544
753
  @see {@link PartialDeep}
@@ -557,10 +766,10 @@ type PartialDeepOptions = {
557
766
  - When set to `true`, elements of non-tuple arrays can be `undefined`.
558
767
  - When set to `false`, only explicitly defined elements are allowed in non-tuple arrays, ensuring stricter type checking.
559
768
 
560
- @default true
769
+ @default false
561
770
 
562
771
  @example
563
- You can prevent `undefined` values in non-tuple arrays by passing `{recurseIntoArrays: true; allowUndefinedInNonTupleArrays: false}` as the second type argument:
772
+ You can allow `undefined` values in non-tuple arrays by passing `{recurseIntoArrays: true; allowUndefinedInNonTupleArrays: true}` as the second type argument:
564
773
 
565
774
  ```
566
775
  import type {PartialDeep} from 'type-fest';
@@ -569,10 +778,9 @@ type PartialDeepOptions = {
569
778
  languages: string[];
570
779
  };
571
780
 
572
- declare const partialSettings: PartialDeep<Settings, {recurseIntoArrays: true; allowUndefinedInNonTupleArrays: false}>;
781
+ declare const partialSettings: PartialDeep<Settings, {recurseIntoArrays: true; allowUndefinedInNonTupleArrays: true}>;
573
782
 
574
- partialSettings.languages = [undefined]; // Error
575
- partialSettings.languages = []; // Ok
783
+ partialSettings.languages = [undefined]; // OK
576
784
  ```
577
785
  */
578
786
  readonly allowUndefinedInNonTupleArrays?: boolean;
@@ -580,33 +788,35 @@ type PartialDeepOptions = {
580
788
 
581
789
  type DefaultPartialDeepOptions = {
582
790
  recurseIntoArrays: false;
583
- allowUndefinedInNonTupleArrays: true;
791
+ allowUndefinedInNonTupleArrays: false;
584
792
  };
585
793
 
586
794
  /**
587
- Create a type from another type with all keys and nested keys set to optional.
795
+ Create a deeply optional version of another type.
588
796
 
589
797
  Use-cases:
590
798
  - Merging a default settings/config object with another object, the second object would be a deep partial of the default object.
591
799
  - Mocking and testing complex entities, where populating an entire object with its keys would be redundant in terms of the mock or test.
592
800
 
801
+ Use [`Partial<T>`](https://www.typescriptlang.org/docs/handbook/utility-types.html#partialtype) if you only need one level deep.
802
+
593
803
  @example
594
804
  ```
595
805
  import type {PartialDeep} from 'type-fest';
596
806
 
597
- const settings: Settings = {
807
+ let settings = {
598
808
  textEditor: {
599
809
  fontSize: 14,
600
810
  fontColor: '#000000',
601
- fontWeight: 400
811
+ fontWeight: 400,
602
812
  },
603
813
  autocomplete: false,
604
- autosave: true
814
+ autosave: true,
605
815
  };
606
816
 
607
- const applySavedSettings = (savedSettings: PartialDeep<Settings>) => {
608
- return {...settings, ...savedSettings};
609
- }
817
+ const applySavedSettings = (savedSettings: PartialDeep<typeof settings>) => (
818
+ {...settings, ...savedSettings, textEditor: {...settings.textEditor, ...savedSettings.textEditor}}
819
+ );
610
820
 
611
821
  settings = applySavedSettings({textEditor: {fontWeight: 500}});
612
822
  ```
@@ -616,13 +826,15 @@ By default, this does not affect elements in array and tuple types. You can chan
616
826
  ```
617
827
  import type {PartialDeep} from 'type-fest';
618
828
 
619
- type Settings = {
620
- languages: string[];
621
- }
829
+ type Shape = {
830
+ dimensions: [number, number];
831
+ };
622
832
 
623
- const partialSettings: PartialDeep<Settings, {recurseIntoArrays: true}> = {
624
- languages: [undefined]
833
+ const partialShape: PartialDeep<Shape, {recurseIntoArrays: true}> = {
834
+ dimensions: [], // OK
625
835
  };
836
+
837
+ partialShape.dimensions = [15]; // OK
626
838
  ```
627
839
 
628
840
  @see {@link PartialDeepOptions}
@@ -637,16 +849,20 @@ type PartialDeep<T, Options extends PartialDeepOptions = {}> =
637
849
 
638
850
  type _PartialDeep<T, Options extends Required<PartialDeepOptions>> = T extends BuiltIns | ((new (...arguments_: any[]) => unknown))
639
851
  ? T
640
- : IsNever<keyof T> extends true // For functions with no properties
641
- ? T
642
- : T extends Map<infer KeyType, infer ValueType>
643
- ? PartialMapDeep<KeyType, ValueType, Options>
644
- : T extends Set<infer ItemType>
645
- ? PartialSetDeep<ItemType, Options>
646
- : T extends ReadonlyMap<infer KeyType, infer ValueType>
647
- ? PartialReadonlyMapDeep<KeyType, ValueType, Options>
648
- : T extends ReadonlySet<infer ItemType>
649
- ? PartialReadonlySetDeep<ItemType, Options>
852
+ : T extends Map<infer KeyType, infer ValueType>
853
+ ? PartialMapDeep<KeyType, ValueType, Options>
854
+ : T extends Set<infer ItemType>
855
+ ? PartialSetDeep<ItemType, Options>
856
+ : T extends ReadonlyMap<infer KeyType, infer ValueType>
857
+ ? PartialReadonlyMapDeep<KeyType, ValueType, Options>
858
+ : T extends ReadonlySet<infer ItemType>
859
+ ? PartialReadonlySetDeep<ItemType, Options>
860
+ : T extends (...arguments_: any[]) => unknown
861
+ ? IsNever<keyof T> extends true
862
+ ? T // For functions with no properties
863
+ : HasMultipleCallSignatures<T> extends true
864
+ ? T
865
+ : ((...arguments_: Parameters<T>) => ReturnType<T>) & PartialObjectDeep<T, Options>
650
866
  : T extends object
651
867
  ? T extends ReadonlyArray<infer ItemType> // Test for arrays/tuples, per https://github.com/microsoft/TypeScript/issues/35156
652
868
  ? Options['recurseIntoArrays'] extends true
@@ -682,12 +898,9 @@ type PartialReadonlySetDeep<T, Options extends Required<PartialDeepOptions>> = {
682
898
  /**
683
899
  Same as `PartialDeep`, but accepts only `object`s as inputs. Internal helper for `PartialDeep`.
684
900
  */
685
- type PartialObjectDeep<ObjectType extends object, Options extends Required<PartialDeepOptions>> =
686
- (ObjectType extends (...arguments_: any) => unknown
687
- ? (...arguments_: Parameters<ObjectType>) => ReturnType<ObjectType>
688
- : {}) & ({
689
- [KeyType in keyof ObjectType]?: _PartialDeep<ObjectType[KeyType], Options>
690
- });
901
+ type PartialObjectDeep<ObjectType extends object, Options extends Required<PartialDeepOptions>> = {
902
+ [KeyType in keyof ObjectType]?: _PartialDeep<ObjectType[KeyType], Options>
903
+ };
691
904
 
692
905
  declare type FallbackProps = {
693
906
  error: unknown;