Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

partial-deep.d.ts 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import {Primitive} from './basic';
  2. /**
  3. Create a type from another type with all keys and nested keys set to optional.
  4. Use-cases:
  5. - Merging a default settings/config object with another object, the second object would be a deep partial of the default object.
  6. - Mocking and testing complex entities, where populating an entire object with its keys would be redundant in terms of the mock or test.
  7. @example
  8. ```
  9. import {PartialDeep} from 'type-fest';
  10. const settings: Settings = {
  11. textEditor: {
  12. fontSize: 14;
  13. fontColor: '#000000';
  14. fontWeight: 400;
  15. }
  16. autocomplete: false;
  17. autosave: true;
  18. };
  19. const applySavedSettings = (savedSettings: PartialDeep<Settings>) => {
  20. return {...settings, ...savedSettings};
  21. }
  22. settings = applySavedSettings({textEditor: {fontWeight: 500}});
  23. ```
  24. */
  25. export type PartialDeep<T> = T extends Primitive
  26. ? Partial<T>
  27. : T extends Map<infer KeyType, infer ValueType>
  28. ? PartialMapDeep<KeyType, ValueType>
  29. : T extends Set<infer ItemType>
  30. ? PartialSetDeep<ItemType>
  31. : T extends ReadonlyMap<infer KeyType, infer ValueType>
  32. ? PartialReadonlyMapDeep<KeyType, ValueType>
  33. : T extends ReadonlySet<infer ItemType>
  34. ? PartialReadonlySetDeep<ItemType>
  35. : T extends ((...arguments: any[]) => unknown)
  36. ? T | undefined
  37. : T extends object
  38. ? PartialObjectDeep<T>
  39. : unknown;
  40. /**
  41. Same as `PartialDeep`, but accepts only `Map`s and as inputs. Internal helper for `PartialDeep`.
  42. */
  43. interface PartialMapDeep<KeyType, ValueType> extends Map<PartialDeep<KeyType>, PartialDeep<ValueType>> {}
  44. /**
  45. Same as `PartialDeep`, but accepts only `Set`s as inputs. Internal helper for `PartialDeep`.
  46. */
  47. interface PartialSetDeep<T> extends Set<PartialDeep<T>> {}
  48. /**
  49. Same as `PartialDeep`, but accepts only `ReadonlyMap`s as inputs. Internal helper for `PartialDeep`.
  50. */
  51. interface PartialReadonlyMapDeep<KeyType, ValueType> extends ReadonlyMap<PartialDeep<KeyType>, PartialDeep<ValueType>> {}
  52. /**
  53. Same as `PartialDeep`, but accepts only `ReadonlySet`s as inputs. Internal helper for `PartialDeep`.
  54. */
  55. interface PartialReadonlySetDeep<T> extends ReadonlySet<PartialDeep<T>> {}
  56. /**
  57. Same as `PartialDeep`, but accepts only `object`s as inputs. Internal helper for `PartialDeep`.
  58. */
  59. type PartialObjectDeep<ObjectType extends object> = {
  60. [KeyType in keyof ObjectType]?: PartialDeep<ObjectType[KeyType]>
  61. };