Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. ############################
  2. # USE TYPE AS KEY OF OBJECT#
  3. ############################
  4. type DayOfTheWeek = "sunday" | "monday" | "tuesday" | "wednesday" | "thursday" | "friday" | "saturday";
  5.  
  6. type ChoresMap = { [day in DayOfTheWeek]: string };
  7.  
  8. const chores: ChoresMap = { // ERROR! Property 'saturday' is missing in type '...'
  9. "sunday": "do the dishes",
  10. "monday": "walk the dog",
  11. "tuesday": "water the plants",
  12. "wednesday": "take out the trash",
  13. "thursday": "clean your room",
  14. "friday": "mow the lawn",
  15. };
  16.  
  17. ##########################################
  18. # USE TYPE AS KEY OF OBJECT with GENERICS#
  19. ##########################################
  20. type DayOfTheWeek = "sunday" | "monday" | "tuesday" | "wednesday" | "thursday" | "friday" | "saturday";
  21.  
  22. type DayOfTheWeekMap<T> = { [day in DayOfTheWeek]: T };
  23.  
  24. const chores: DayOfTheWeekMap<string> = {
  25. "sunday": "do the dishes",
  26. "monday": "walk the dog",
  27. "tuesday": "water the plants",
  28. "wednesday": "take out the trash",
  29. "thursday": "clean your room",
  30. "friday": "mow the lawn",
  31. "saturday": "relax",
  32. };
  33.  
  34. const workDays: DayOfTheWeekMap<boolean> = {
  35. "sunday": false,
  36. "monday": true,
  37. "tuesday": true,
  38. "wednesday": true,
  39. "thursday": true,
  40. "friday": true,
  41. "saturday": false,
  42. }
  43.  
  44. #########################
  45. #EXCLUDE ITEMS FROM TYPE#
  46. #########################
  47.  
  48. interface CreditCardOwnerInformationUIDTO {
  49. floor: string;
  50. department: string;
  51. postalCode: string;
  52. }
  53.  
  54. type TGetValues = Pick<
  55. CreditCardOwnerInformationUIDTO,
  56. Exclude<keyof CreditCardOwnerInformationUIDTO, 'floor' | 'department'>
  57. >;
  58.  
  59. // TGetValues should be = postalCode: string;
  60.  
  61.  
  62. ##################################
  63. # UNION TYPE BASED ON OBJECT KEYS#
  64. ##################################
  65.  
  66. const paymentTypes = {
  67. prepaid: 'foo',
  68. at_destination: 'bar',
  69. advance_payment: 'foo bar',
  70. };
  71.  
  72. type p = keyof typeof paymentTypes;
  73.  
  74. // P must be = "prepaid" | "at_destination" | "advance_payment";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement