@wix/sdk-types 1.13.5 → 1.13.7

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.
package/build/index.d.mts CHANGED
@@ -224,135 +224,166 @@ Unfortunately, `Record<string, never>`, `Record<keyof any, never>` and `Record<n
224
224
  type EmptyObject = {[emptyObjectSymbol]?: never};
225
225
 
226
226
  /**
227
- Returns a boolean for whether the two given types are equal.
227
+ Extract all required keys from the given type.
228
228
 
229
- @link https://github.com/microsoft/TypeScript/issues/27024#issuecomment-421529650
230
- @link https://stackoverflow.com/questions/68961864/how-does-the-equals-work-in-typescript/68963796#68963796
231
-
232
- Use-cases:
233
- - If you want to make a conditional branch based on the result of a comparison of two types.
229
+ This is useful when you want to create a new type that contains different type values for the required keys only or use the list of keys for validation purposes, etc...
234
230
 
235
231
  @example
236
232
  ```
237
- import type {IsEqual} from 'type-fest';
233
+ import type {RequiredKeysOf} from 'type-fest';
238
234
 
239
- // This type returns a boolean for whether the given array includes the given item.
240
- // `IsEqual` is used to compare the given array at position 0 and the given item and then return true if they are equal.
241
- type Includes<Value extends readonly any[], Item> =
242
- Value extends readonly [Value[0], ...infer rest]
243
- ? IsEqual<Value[0], Item> extends true
244
- ? true
245
- : Includes<rest, Item>
246
- : false;
235
+ declare function createValidation<Entity extends object, Key extends RequiredKeysOf<Entity> = RequiredKeysOf<Entity>>(field: Key, validator: (value: Entity[Key]) => boolean): ValidatorFn;
236
+
237
+ interface User {
238
+ name: string;
239
+ surname: string;
240
+
241
+ luckyNumber?: number;
242
+ }
243
+
244
+ const validator1 = createValidation<User>('name', value => value.length < 25);
245
+ const validator2 = createValidation<User>('surname', value => value.length < 25);
247
246
  ```
248
247
 
249
- @category Type Guard
250
248
  @category Utilities
251
249
  */
252
- type IsEqual<A, B> =
253
- (<G>() => G extends A & G | G ? 1 : 2) extends
254
- (<G>() => G extends B & G | G ? 1 : 2)
255
- ? true
256
- : false;
250
+ type RequiredKeysOf<BaseType extends object> = Exclude<{
251
+ [Key in keyof BaseType]: BaseType extends Record<Key, BaseType[Key]>
252
+ ? Key
253
+ : never
254
+ }[keyof BaseType], undefined>;
257
255
 
258
256
  /**
259
- Filter out keys from an object.
257
+ Returns a boolean for whether the given type is `never`.
260
258
 
261
- Returns `never` if `Exclude` is strictly equal to `Key`.
262
- Returns `never` if `Key` extends `Exclude`.
263
- Returns `Key` otherwise.
259
+ @link https://github.com/microsoft/TypeScript/issues/31751#issuecomment-498526919
260
+ @link https://stackoverflow.com/a/53984913/10292952
261
+ @link https://www.zhenghao.io/posts/ts-never
264
262
 
265
- @example
266
- ```
267
- type Filtered = Filter<'foo', 'foo'>;
268
- //=> never
269
- ```
263
+ Useful in type utilities, such as checking if something does not occur.
270
264
 
271
265
  @example
272
266
  ```
273
- type Filtered = Filter<'bar', string>;
267
+ import type {IsNever, And} from 'type-fest';
268
+
269
+ // https://github.com/andnp/SimplyTyped/blob/master/src/types/strings.ts
270
+ type AreStringsEqual<A extends string, B extends string> =
271
+ And<
272
+ IsNever<Exclude<A, B>> extends true ? true : false,
273
+ IsNever<Exclude<B, A>> extends true ? true : false
274
+ >;
275
+
276
+ type EndIfEqual<I extends string, O extends string> =
277
+ AreStringsEqual<I, O> extends true
278
+ ? never
279
+ : void;
280
+
281
+ function endIfEqual<I extends string, O extends string>(input: I, output: O): EndIfEqual<I, O> {
282
+ if (input === output) {
283
+ process.exit(0);
284
+ }
285
+ }
286
+
287
+ endIfEqual('abc', 'abc');
274
288
  //=> never
289
+
290
+ endIfEqual('abc', '123');
291
+ //=> void
275
292
  ```
276
293
 
294
+ @category Type Guard
295
+ @category Utilities
296
+ */
297
+ type IsNever<T> = [T] extends [never] ? true : false;
298
+
299
+ /**
300
+ An if-else-like type that resolves depending on whether the given type is `never`.
301
+
302
+ @see {@link IsNever}
303
+
277
304
  @example
278
305
  ```
279
- type Filtered = Filter<'bar', 'foo'>;
306
+ import type {IfNever} from 'type-fest';
307
+
308
+ type ShouldBeTrue = IfNever<never>;
309
+ //=> true
310
+
311
+ type ShouldBeBar = IfNever<'not never', 'foo', 'bar'>;
280
312
  //=> 'bar'
281
313
  ```
282
314
 
283
- @see {Except}
315
+ @category Type Guard
316
+ @category Utilities
284
317
  */
285
- type Filter<KeyType, ExcludeType> = IsEqual<KeyType, ExcludeType> extends true ? never : (KeyType extends ExcludeType ? never : KeyType);
286
-
287
- type ExceptOptions = {
288
- /**
289
- Disallow assigning non-specified properties.
290
-
291
- Note that any omitted properties in the resulting type will be present in autocomplete as `undefined`.
318
+ type IfNever<T, TypeIfNever = true, TypeIfNotNever = false> = (
319
+ IsNever<T> extends true ? TypeIfNever : TypeIfNotNever
320
+ );
292
321
 
293
- @default false
294
- */
295
- requireExactProps?: boolean;
296
- };
322
+ // Can eventually be replaced with the built-in once this library supports
323
+ // TS5.4+ only. Tracked in https://github.com/sindresorhus/type-fest/issues/848
324
+ type NoInfer<T> = T extends infer U ? U : never;
297
325
 
298
326
  /**
299
- Create a type from an object type without certain keys.
327
+ Returns a boolean for whether the given type is `any`.
300
328
 
301
- We recommend setting the `requireExactProps` option to `true`.
302
-
303
- This type is a stricter version of [`Omit`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-5.html#the-omit-helper-type). The `Omit` type does not restrict the omitted keys to be keys present on the given type, while `Except` does. The benefits of a stricter type are avoiding typos and allowing the compiler to pick up on rename refactors automatically.
329
+ @link https://stackoverflow.com/a/49928360/1490091
304
330
 
305
- This type was proposed to the TypeScript team, which declined it, saying they prefer that libraries implement stricter versions of the built-in types ([microsoft/TypeScript#30825](https://github.com/microsoft/TypeScript/issues/30825#issuecomment-523668235)).
331
+ Useful in type utilities, such as disallowing `any`s to be passed to a function.
306
332
 
307
333
  @example
308
334
  ```
309
- import type {Except} from 'type-fest';
335
+ import type {IsAny} from 'type-fest';
310
336
 
311
- type Foo = {
312
- a: number;
313
- b: string;
314
- };
337
+ const typedObject = {a: 1, b: 2} as const;
338
+ const anyObject: any = {a: 1, b: 2};
315
339
 
316
- type FooWithoutA = Except<Foo, 'a'>;
317
- //=> {b: string}
340
+ function get<O extends (IsAny<O> extends true ? {} : Record<string, number>), K extends keyof O = keyof O>(obj: O, key: K) {
341
+ return obj[key];
342
+ }
318
343
 
319
- const fooWithoutA: FooWithoutA = {a: 1, b: '2'};
320
- //=> errors: 'a' does not exist in type '{ b: string; }'
344
+ const typedA = get(typedObject, 'a');
345
+ //=> 1
321
346
 
322
- type FooWithoutB = Except<Foo, 'b', {requireExactProps: true}>;
323
- //=> {a: number} & Partial<Record<"b", never>>
347
+ const anyA = get(anyObject, 'a');
348
+ //=> any
349
+ ```
324
350
 
325
- const fooWithoutB: FooWithoutB = {a: 1, b: '2'};
326
- //=> errors at 'b': Type 'string' is not assignable to type 'undefined'.
351
+ @category Type Guard
352
+ @category Utilities
353
+ */
354
+ type IsAny<T> = 0 extends 1 & NoInfer<T> ? true : false;
327
355
 
328
- // The `Omit` utility type doesn't work when omitting specific keys from objects containing index signatures.
356
+ /**
357
+ Returns a boolean for whether the two given types are equal.
329
358
 
330
- // Consider the following example:
359
+ @link https://github.com/microsoft/TypeScript/issues/27024#issuecomment-421529650
360
+ @link https://stackoverflow.com/questions/68961864/how-does-the-equals-work-in-typescript/68963796#68963796
331
361
 
332
- type UserData = {
333
- [metadata: string]: string;
334
- email: string;
335
- name: string;
336
- role: 'admin' | 'user';
337
- };
362
+ Use-cases:
363
+ - If you want to make a conditional branch based on the result of a comparison of two types.
338
364
 
339
- // `Omit` clearly doesn't behave as expected in this case:
340
- type PostPayload = Omit<UserData, 'email'>;
341
- //=> type PostPayload = { [x: string]: string; [x: number]: string; }
365
+ @example
366
+ ```
367
+ import type {IsEqual} from 'type-fest';
342
368
 
343
- // In situations like this, `Except` works better.
344
- // It simply removes the `email` key while preserving all the other keys.
345
- type PostPayload = Except<UserData, 'email'>;
346
- //=> type PostPayload = { [x: string]: string; name: string; role: 'admin' | 'user'; }
369
+ // This type returns a boolean for whether the given array includes the given item.
370
+ // `IsEqual` is used to compare the given array at position 0 and the given item and then return true if they are equal.
371
+ type Includes<Value extends readonly any[], Item> =
372
+ Value extends readonly [Value[0], ...infer rest]
373
+ ? IsEqual<Value[0], Item> extends true
374
+ ? true
375
+ : Includes<rest, Item>
376
+ : false;
347
377
  ```
348
378
 
349
- @category Object
379
+ @category Type Guard
380
+ @category Utilities
350
381
  */
351
- type Except<ObjectType, KeysType extends keyof ObjectType, Options extends ExceptOptions = {requireExactProps: false}> = {
352
- [KeyType in keyof ObjectType as Filter<KeyType, KeysType>]: ObjectType[KeyType];
353
- } & (Options['requireExactProps'] extends true
354
- ? Partial<Record<KeysType, never>>
355
- : {});
382
+ type IsEqual<A, B> =
383
+ (<G>() => G extends A & G | G ? 1 : 2) extends
384
+ (<G>() => G extends B & G | G ? 1 : 2)
385
+ ? true
386
+ : false;
356
387
 
357
388
  /**
358
389
  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.
@@ -414,71 +445,431 @@ fn(someInterface as Simplify<SomeInterface>); // Good: transform an `interface`
414
445
  type Simplify<T> = {[KeyType in keyof T]: T[KeyType]} & {};
415
446
 
416
447
  /**
417
- Returns a boolean for whether the given type is `never`.
448
+ Omit any index signatures from the given object type, leaving only explicitly defined properties.
418
449
 
419
- @link https://github.com/microsoft/TypeScript/issues/31751#issuecomment-498526919
420
- @link https://stackoverflow.com/a/53984913/10292952
421
- @link https://www.zhenghao.io/posts/ts-never
450
+ This is the counterpart of `PickIndexSignature`.
422
451
 
423
- Useful in type utilities, such as checking if something does not occur.
452
+ Use-cases:
453
+ - Remove overly permissive signatures from third-party types.
454
+
455
+ This type was taken from this [StackOverflow answer](https://stackoverflow.com/a/68261113/420747).
456
+
457
+ It relies on the fact that an empty object (`{}`) is assignable to an object with just an index signature, like `Record<string, unknown>`, but not to an object with explicitly defined keys, like `Record<'foo' | 'bar', unknown>`.
458
+
459
+ (The actual value type, `unknown`, is irrelevant and could be any type. Only the key type matters.)
460
+
461
+ ```
462
+ const indexed: Record<string, unknown> = {}; // Allowed
463
+
464
+ const keyed: Record<'foo', unknown> = {}; // Error
465
+ // => TS2739: Type '{}' is missing the following properties from type 'Record<"foo" | "bar", unknown>': foo, bar
466
+ ```
467
+
468
+ 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:
469
+
470
+ ```
471
+ type Indexed = {} extends Record<string, unknown>
472
+ ? '✅ `{}` is assignable to `Record<string, unknown>`'
473
+ : '❌ `{}` is NOT assignable to `Record<string, unknown>`';
474
+ // => '✅ `{}` is assignable to `Record<string, unknown>`'
475
+
476
+ type Keyed = {} extends Record<'foo' | 'bar', unknown>
477
+ ? "✅ `{}` is assignable to `Record<'foo' | 'bar', unknown>`"
478
+ : "❌ `{}` is NOT assignable to `Record<'foo' | 'bar', unknown>`";
479
+ // => "❌ `{}` is NOT assignable to `Record<'foo' | 'bar', unknown>`"
480
+ ```
481
+
482
+ 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`...
483
+
484
+ ```
485
+ import type {OmitIndexSignature} from 'type-fest';
486
+
487
+ type OmitIndexSignature<ObjectType> = {
488
+ [KeyType in keyof ObjectType // Map each key of `ObjectType`...
489
+ ]: ObjectType[KeyType]; // ...to its original value, i.e. `OmitIndexSignature<Foo> == Foo`.
490
+ };
491
+ ```
492
+
493
+ ...whether an empty object (`{}`) would be assignable to an object with that `KeyType` (`Record<KeyType, unknown>`)...
494
+
495
+ ```
496
+ import type {OmitIndexSignature} from 'type-fest';
497
+
498
+ type OmitIndexSignature<ObjectType> = {
499
+ [KeyType in keyof ObjectType
500
+ // Is `{}` assignable to `Record<KeyType, unknown>`?
501
+ as {} extends Record<KeyType, unknown>
502
+ ? ... // ✅ `{}` is assignable to `Record<KeyType, unknown>`
503
+ : ... // ❌ `{}` is NOT assignable to `Record<KeyType, unknown>`
504
+ ]: ObjectType[KeyType];
505
+ };
506
+ ```
507
+
508
+ If `{}` is assignable, it means that `KeyType` is an index signature and we want to remove it. If it is not assignable, `KeyType` is a "real" key and we want to keep it.
424
509
 
425
510
  @example
426
511
  ```
427
- import type {IsNever, And} from 'type-fest';
512
+ import type {OmitIndexSignature} from 'type-fest';
428
513
 
429
- // https://github.com/andnp/SimplyTyped/blob/master/src/types/strings.ts
430
- type AreStringsEqual<A extends string, B extends string> =
431
- And<
432
- IsNever<Exclude<A, B>> extends true ? true : false,
433
- IsNever<Exclude<B, A>> extends true ? true : false
434
- >;
514
+ interface Example {
515
+ // These index signatures will be removed.
516
+ [x: string]: any
517
+ [x: number]: any
518
+ [x: symbol]: any
519
+ [x: `head-${string}`]: string
520
+ [x: `${string}-tail`]: string
521
+ [x: `head-${string}-tail`]: string
522
+ [x: `${bigint}`]: string
523
+ [x: `embedded-${number}`]: string
524
+
525
+ // These explicitly defined keys will remain.
526
+ foo: 'bar';
527
+ qux?: 'baz';
528
+ }
435
529
 
436
- type EndIfEqual<I extends string, O extends string> =
437
- AreStringsEqual<I, O> extends true
530
+ type ExampleWithoutIndexSignatures = OmitIndexSignature<Example>;
531
+ // => { foo: 'bar'; qux?: 'baz' | undefined; }
532
+ ```
533
+
534
+ @see PickIndexSignature
535
+ @category Object
536
+ */
537
+ type OmitIndexSignature<ObjectType> = {
538
+ [KeyType in keyof ObjectType as {} extends Record<KeyType, unknown>
438
539
  ? never
439
- : void;
540
+ : KeyType]: ObjectType[KeyType];
541
+ };
440
542
 
441
- function endIfEqual<I extends string, O extends string>(input: I, output: O): EndIfEqual<I, O> {
442
- if (input === output) {
443
- process.exit(0);
444
- }
543
+ /**
544
+ Pick only index signatures from the given object type, leaving out all explicitly defined properties.
545
+
546
+ This is the counterpart of `OmitIndexSignature`.
547
+
548
+ @example
549
+ ```
550
+ import type {PickIndexSignature} from 'type-fest';
551
+
552
+ declare const symbolKey: unique symbol;
553
+
554
+ type Example = {
555
+ // These index signatures will remain.
556
+ [x: string]: unknown;
557
+ [x: number]: unknown;
558
+ [x: symbol]: unknown;
559
+ [x: `head-${string}`]: string;
560
+ [x: `${string}-tail`]: string;
561
+ [x: `head-${string}-tail`]: string;
562
+ [x: `${bigint}`]: string;
563
+ [x: `embedded-${number}`]: string;
564
+
565
+ // These explicitly defined keys will be removed.
566
+ ['kebab-case-key']: string;
567
+ [symbolKey]: string;
568
+ foo: 'bar';
569
+ qux?: 'baz';
570
+ };
571
+
572
+ type ExampleIndexSignature = PickIndexSignature<Example>;
573
+ // {
574
+ // [x: string]: unknown;
575
+ // [x: number]: unknown;
576
+ // [x: symbol]: unknown;
577
+ // [x: `head-${string}`]: string;
578
+ // [x: `${string}-tail`]: string;
579
+ // [x: `head-${string}-tail`]: string;
580
+ // [x: `${bigint}`]: string;
581
+ // [x: `embedded-${number}`]: string;
582
+ // }
583
+ ```
584
+
585
+ @see OmitIndexSignature
586
+ @category Object
587
+ */
588
+ type PickIndexSignature<ObjectType> = {
589
+ [KeyType in keyof ObjectType as {} extends Record<KeyType, unknown>
590
+ ? KeyType
591
+ : never]: ObjectType[KeyType];
592
+ };
593
+
594
+ // Merges two objects without worrying about index signatures.
595
+ type SimpleMerge<Destination, Source> = {
596
+ [Key in keyof Destination as Key extends keyof Source ? never : Key]: Destination[Key];
597
+ } & Source;
598
+
599
+ /**
600
+ Merge two types into a new type. Keys of the second type overrides keys of the first type.
601
+
602
+ @example
603
+ ```
604
+ import type {Merge} from 'type-fest';
605
+
606
+ interface Foo {
607
+ [x: string]: unknown;
608
+ [x: number]: unknown;
609
+ foo: string;
610
+ bar: symbol;
445
611
  }
446
612
 
447
- endIfEqual('abc', 'abc');
448
- //=> never
613
+ type Bar = {
614
+ [x: number]: number;
615
+ [x: symbol]: unknown;
616
+ bar: Date;
617
+ baz: boolean;
618
+ };
449
619
 
450
- endIfEqual('abc', '123');
451
- //=> void
620
+ export type FooBar = Merge<Foo, Bar>;
621
+ // => {
622
+ // [x: string]: unknown;
623
+ // [x: number]: number;
624
+ // [x: symbol]: unknown;
625
+ // foo: string;
626
+ // bar: Date;
627
+ // baz: boolean;
628
+ // }
452
629
  ```
453
630
 
454
- @category Type Guard
455
- @category Utilities
631
+ @category Object
456
632
  */
457
- type IsNever<T> = [T] extends [never] ? true : false;
633
+ type Merge<Destination, Source> =
634
+ Simplify<
635
+ SimpleMerge<PickIndexSignature<Destination>, PickIndexSignature<Source>>
636
+ & SimpleMerge<OmitIndexSignature<Destination>, OmitIndexSignature<Source>>
637
+ >;
458
638
 
459
639
  /**
460
- An if-else-like type that resolves depending on whether the given type is `never`.
640
+ An if-else-like type that resolves depending on whether the given type is `any`.
461
641
 
462
- @see {@link IsNever}
642
+ @see {@link IsAny}
463
643
 
464
644
  @example
465
645
  ```
466
- import type {IfNever} from 'type-fest';
646
+ import type {IfAny} from 'type-fest';
467
647
 
468
- type ShouldBeTrue = IfNever<never>;
648
+ type ShouldBeTrue = IfAny<any>;
469
649
  //=> true
470
650
 
471
- type ShouldBeBar = IfNever<'not never', 'foo', 'bar'>;
651
+ type ShouldBeBar = IfAny<'not any', 'foo', 'bar'>;
472
652
  //=> 'bar'
473
653
  ```
474
654
 
475
655
  @category Type Guard
476
656
  @category Utilities
477
657
  */
478
- type IfNever<T, TypeIfNever = true, TypeIfNotNever = false> = (
479
- IsNever<T> extends true ? TypeIfNever : TypeIfNotNever
658
+ type IfAny<T, TypeIfAny = true, TypeIfNotAny = false> = (
659
+ IsAny<T> extends true ? TypeIfAny : TypeIfNotAny
480
660
  );
481
661
 
662
+ /**
663
+ Extract all optional keys from the given type.
664
+
665
+ This is useful when you want to create a new type that contains different type values for the optional keys only.
666
+
667
+ @example
668
+ ```
669
+ import type {OptionalKeysOf, Except} from 'type-fest';
670
+
671
+ interface User {
672
+ name: string;
673
+ surname: string;
674
+
675
+ luckyNumber?: number;
676
+ }
677
+
678
+ const REMOVE_FIELD = Symbol('remove field symbol');
679
+ type UpdateOperation<Entity extends object> = Except<Partial<Entity>, OptionalKeysOf<Entity>> & {
680
+ [Key in OptionalKeysOf<Entity>]?: Entity[Key] | typeof REMOVE_FIELD;
681
+ };
682
+
683
+ const update1: UpdateOperation<User> = {
684
+ name: 'Alice'
685
+ };
686
+
687
+ const update2: UpdateOperation<User> = {
688
+ name: 'Bob',
689
+ luckyNumber: REMOVE_FIELD
690
+ };
691
+ ```
692
+
693
+ @category Utilities
694
+ */
695
+ type OptionalKeysOf<BaseType extends object> = Exclude<{
696
+ [Key in keyof BaseType]: BaseType extends Record<Key, BaseType[Key]>
697
+ ? never
698
+ : Key
699
+ }[keyof BaseType], undefined>;
700
+
701
+ /**
702
+ Merges user specified options with default options.
703
+
704
+ @example
705
+ ```
706
+ type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
707
+ type DefaultPathsOptions = {maxRecursionDepth: 10; leavesOnly: false};
708
+ type SpecifiedOptions = {leavesOnly: true};
709
+
710
+ type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
711
+ //=> {maxRecursionDepth: 10; leavesOnly: true}
712
+ ```
713
+
714
+ @example
715
+ ```
716
+ // Complains if default values are not provided for optional options
717
+
718
+ type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
719
+ type DefaultPathsOptions = {maxRecursionDepth: 10};
720
+ type SpecifiedOptions = {};
721
+
722
+ type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
723
+ // ~~~~~~~~~~~~~~~~~~~
724
+ // Property 'leavesOnly' is missing in type 'DefaultPathsOptions' but required in type '{ maxRecursionDepth: number; leavesOnly: boolean; }'.
725
+ ```
726
+
727
+ @example
728
+ ```
729
+ // Complains if an option's default type does not conform to the expected type
730
+
731
+ type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
732
+ type DefaultPathsOptions = {maxRecursionDepth: 10; leavesOnly: 'no'};
733
+ type SpecifiedOptions = {};
734
+
735
+ type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
736
+ // ~~~~~~~~~~~~~~~~~~~
737
+ // Types of property 'leavesOnly' are incompatible. Type 'string' is not assignable to type 'boolean'.
738
+ ```
739
+
740
+ @example
741
+ ```
742
+ // Complains if an option's specified type does not conform to the expected type
743
+
744
+ type PathsOptions = {maxRecursionDepth?: number; leavesOnly?: boolean};
745
+ type DefaultPathsOptions = {maxRecursionDepth: 10; leavesOnly: false};
746
+ type SpecifiedOptions = {leavesOnly: 'yes'};
747
+
748
+ type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOptions>;
749
+ // ~~~~~~~~~~~~~~~~
750
+ // Types of property 'leavesOnly' are incompatible. Type 'string' is not assignable to type 'boolean'.
751
+ ```
752
+ */
753
+ type ApplyDefaultOptions<
754
+ Options extends object,
755
+ Defaults extends Simplify<Omit<Required<Options>, RequiredKeysOf<Options>> & Partial<Record<RequiredKeysOf<Options>, never>>>,
756
+ SpecifiedOptions extends Options,
757
+ > =
758
+ IfAny<SpecifiedOptions, Defaults,
759
+ IfNever<SpecifiedOptions, Defaults,
760
+ Simplify<Merge<Defaults, {
761
+ [Key in keyof SpecifiedOptions
762
+ as Key extends OptionalKeysOf<Options> ? undefined extends SpecifiedOptions[Key] ? never : Key : Key
763
+ ]: SpecifiedOptions[Key]
764
+ }> & Required<Options>> // `& Required<Options>` ensures that `ApplyDefaultOptions<SomeOption, ...>` is always assignable to `Required<SomeOption>`
765
+ >>;
766
+
767
+ /**
768
+ Filter out keys from an object.
769
+
770
+ Returns `never` if `Exclude` is strictly equal to `Key`.
771
+ Returns `never` if `Key` extends `Exclude`.
772
+ Returns `Key` otherwise.
773
+
774
+ @example
775
+ ```
776
+ type Filtered = Filter<'foo', 'foo'>;
777
+ //=> never
778
+ ```
779
+
780
+ @example
781
+ ```
782
+ type Filtered = Filter<'bar', string>;
783
+ //=> never
784
+ ```
785
+
786
+ @example
787
+ ```
788
+ type Filtered = Filter<'bar', 'foo'>;
789
+ //=> 'bar'
790
+ ```
791
+
792
+ @see {Except}
793
+ */
794
+ type Filter<KeyType, ExcludeType> = IsEqual<KeyType, ExcludeType> extends true ? never : (KeyType extends ExcludeType ? never : KeyType);
795
+
796
+ type ExceptOptions = {
797
+ /**
798
+ Disallow assigning non-specified properties.
799
+
800
+ Note that any omitted properties in the resulting type will be present in autocomplete as `undefined`.
801
+
802
+ @default false
803
+ */
804
+ requireExactProps?: boolean;
805
+ };
806
+
807
+ type DefaultExceptOptions = {
808
+ requireExactProps: false;
809
+ };
810
+
811
+ /**
812
+ Create a type from an object type without certain keys.
813
+
814
+ We recommend setting the `requireExactProps` option to `true`.
815
+
816
+ This type is a stricter version of [`Omit`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-5.html#the-omit-helper-type). The `Omit` type does not restrict the omitted keys to be keys present on the given type, while `Except` does. The benefits of a stricter type are avoiding typos and allowing the compiler to pick up on rename refactors automatically.
817
+
818
+ This type was proposed to the TypeScript team, which declined it, saying they prefer that libraries implement stricter versions of the built-in types ([microsoft/TypeScript#30825](https://github.com/microsoft/TypeScript/issues/30825#issuecomment-523668235)).
819
+
820
+ @example
821
+ ```
822
+ import type {Except} from 'type-fest';
823
+
824
+ type Foo = {
825
+ a: number;
826
+ b: string;
827
+ };
828
+
829
+ type FooWithoutA = Except<Foo, 'a'>;
830
+ //=> {b: string}
831
+
832
+ const fooWithoutA: FooWithoutA = {a: 1, b: '2'};
833
+ //=> errors: 'a' does not exist in type '{ b: string; }'
834
+
835
+ type FooWithoutB = Except<Foo, 'b', {requireExactProps: true}>;
836
+ //=> {a: number} & Partial<Record<"b", never>>
837
+
838
+ const fooWithoutB: FooWithoutB = {a: 1, b: '2'};
839
+ //=> errors at 'b': Type 'string' is not assignable to type 'undefined'.
840
+
841
+ // The `Omit` utility type doesn't work when omitting specific keys from objects containing index signatures.
842
+
843
+ // Consider the following example:
844
+
845
+ type UserData = {
846
+ [metadata: string]: string;
847
+ email: string;
848
+ name: string;
849
+ role: 'admin' | 'user';
850
+ };
851
+
852
+ // `Omit` clearly doesn't behave as expected in this case:
853
+ type PostPayload = Omit<UserData, 'email'>;
854
+ //=> type PostPayload = { [x: string]: string; [x: number]: string; }
855
+
856
+ // In situations like this, `Except` works better.
857
+ // It simply removes the `email` key while preserving all the other keys.
858
+ type PostPayload = Except<UserData, 'email'>;
859
+ //=> type PostPayload = { [x: string]: string; name: string; role: 'admin' | 'user'; }
860
+ ```
861
+
862
+ @category Object
863
+ */
864
+ type Except<ObjectType, KeysType extends keyof ObjectType, Options extends ExceptOptions = {}> =
865
+ _Except<ObjectType, KeysType, ApplyDefaultOptions<ExceptOptions, DefaultExceptOptions, Options>>;
866
+
867
+ type _Except<ObjectType, KeysType extends keyof ObjectType, Options extends Required<ExceptOptions>> = {
868
+ [KeyType in keyof ObjectType as Filter<KeyType, KeysType>]: ObjectType[KeyType];
869
+ } & (Options['requireExactProps'] extends true
870
+ ? Partial<Record<KeysType, never>>
871
+ : {});
872
+
482
873
  /**
483
874
  Extract the keys from a type where the value type of the key extends the given `Condition`.
484
875