@wix/sdk-types 1.13.6 → 1.13.8

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