{"id":3172,"date":"2021-10-29T12:08:22","date_gmt":"2021-10-29T20:08:22","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/typescript\/?p=3172"},"modified":"2023-04-27T13:18:43","modified_gmt":"2023-04-27T21:18:43","slug":"type-treat-day-5","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/typescript\/type-treat-day-5\/","title":{"rendered":"Type | Treat &#8211; Day 5"},"content":{"rendered":"<h2>Type | Treat Challenge 5<\/h2>\n<p>Welcome to the fifth, and last, <code>Type | Treat<\/code> challenge! These challenges are a series of blog posts which have 2 code challenges in, one for beginners and one for intermediate TypeScript programmers. We&#8217;re on day five, which means going over the answers from <a href=\"https:\/\/devblogs.microsoft.com\/typescript\/type-treat-2021-day-4\/\" rel=\"nofollow\">yesterday<\/a> and have 2 new challenges.<\/p>\n<h2>Yesterday&#8217;s Solution<\/h2>\n<h3>Beginner\/Learner Challenge<\/h3>\n<p>This challenge aimed to be generics 101, first introducing the concept of making your function pass a type from the function to the argument:<\/p>\n<div class=\"highlight highlight-source-diff\">\n<pre><span class=\"pl-md\"><span class=\"pl-md\">-<\/span>  function getBowl(items: any) {<\/span>\r\n<span class=\"pl-mi1\"><span class=\"pl-mi1\">+<\/span>  function getBowl&lt;T&gt;(items: T) {<\/span>\r\n      return { items }\r\n  }<\/pre>\n<\/div>\n<p>The <code>any<\/code> acted as hint clue about where to look, and this example is almost the first code sample on the <a href=\"https:\/\/www.typescriptlang.org\/docs\/handbook\/2\/generics.html\" rel=\"nofollow\">Generics chapter<\/a> in the Handbook, so it felt like a good intro.<\/p>\n<p>The second part involved understanding <a href=\"https:\/\/www.typescriptlang.org\/docs\/handbook\/2\/generics.html#generic-constraints\" rel=\"nofollow\">generic constraints<\/a>, these are essential tools in helping you define the baselines for types which can be used in your function. In this case we didn&#8217;t provide the word &#8220;constraints&#8221; but opted for a more cryptic clue by setting up the function most of the way, then saying you only needed two words:<\/p>\n<div class=\"highlight highlight-source-diff\">\n<pre><span class=\"pl-md\"><span class=\"pl-md\">-<\/span>  function fillBowl&lt;T&gt;(candy: T) {<\/span>\r\n<span class=\"pl-mi1\"><span class=\"pl-mi1\">+<\/span>  function fillBowl&lt;T extends string&gt;(candy: T) {<\/span>\r\n      return { candy }\r\n  }<\/pre>\n<\/div>\n<p>By saying that <code>T<\/code> extended <code>string<\/code> then the string literals are correctly passed through the function &#8211; which removes all the compiler errors.<\/p>\n<p><a href=\"https:\/\/www.typescriptlang.org\/play?#code\/PTAEE0HsFcHIDcCmoDmkAuoCeMBOBnRAGwDNQAjaAOwGMALUSM8yAdyP0bJoEMqATLABoK0TOjrIW7Tj1zIAUCFAAraPkwAiFIiqJcASxqaKbDgDoIMULyqgADmNB8sEg1RSh3oCYgC2Inz8qIiYLm4eSmDkPDQA1oxOTD6SfpYKCiTUNOgGkHY66ABCZgA8ACoAfAAUBuj++ABcoOUAlKAA3gqgPaDy6NC4dh1e9X6cAL4KUwo0+Ro2kqxYALIGAB4l7KAAvCHFZtUjc0SQBM0A2pry-Joi2vK6d6Ca5ETQiJoAuiIauDyscj6QyIJovfCQPyfUATVpRXo9AB6AH4MnMqAtbPwDIh+ABBez2IigrZEXb7UlHHxYeygy6acp8FB0aDvZ6aFY8ADnAGPQPxIM4-Gd2eUmCREMgCUTQd8YXDlAjQCiMspynQDJxNc5QPg6DwoSIgbx1MgDPxEDwiEQsCltawYERgv9NcgeDZIfYDMTcM5cLgzo00UEcfjCcT8KTzHUGuZ0DTEBcAAxfVVgADCfGw1giCWS7tYPFt6EFxPEkgccgNoX0XFAAANCqT66A4pL7ClkPHaaB4cl6zHxi2JAHoMzO6AsrRcvlURl5wv4QBleyQb36ThA06sZyySu4TDJXygRDreoYvJ2I8azing0yxcL5RL3LWnXkOqTwa+X0kM4pSBCEYOwiDkHRdRoR4MUfeF00gSAiBEH8zUwTVF2UKA4CQVAMGzQZCFIURaAYZJpA4OssWEURyykMxZHkXtlDUBZtF0fQjBMMj8EsTDUH+cgdQlHcSD4GhbXIf4BFxUBGnQPxmi45wBHhEhvTJY9FNYOoGF8AxfUo8wMinHJL0nNTSQqE8z10fhOD+dwUBqSjmjaTpul6fpBmGGwgltKYZnRBY-DkSMzHJVTrUpTRgoITQ4UCzB8CoIw2wIUlwvMw5NCSlKNzi2Z5kSuI6nQCN0r2CKiCi-BivQUrZTheEAElxC8zgYHLHgwknRAd2K2y-WQL14mLOgYGZIR4SCYCbVAQsqHEQVCmArsEzrHyBFtEgAz8E9YgYMjDIKjFMBQAwkAAeTEJdkviDdyWqP5mmy27UvwOLdkqToYWOhYzsu67avqzg9ke9BcGemqSojD6di+kYpgS1BzsQK70E5NKQoep6Xhi972jh76ZjVG9QChPhOBwaA5rU0A9GkksrPPYJj3rSrmx8SB4X1ARiUrfB8AcnV8FpGgDFUmhdXBoWiBjf51LoUdx18eFj2MmcqB46wtLffJZvplnBR4fgWYdOazgGxnVYrdXTMZ4K2ztTgHVwOIjv+1HrtejdHu9tKzHMSi4Q9tGlyBiNHvDkkA6DhQQ7EDHIxC6o8ajWOMiAA\" rel=\"nofollow\">Our answer<\/a><\/p>\n<h3>Intermediate\/Advanced Challenge<\/h3>\n<p>The intermediate challenge also invovled generic constraints, so if you had just finished the beginner&#8217;s then you were in a good place to figure this challenge. The key is to make a<\/p>\n<div class=\"highlight highlight-source-diff\">\n<pre><span class=\"pl-md\"><span class=\"pl-md\">-<\/span> const check = (data: Competitor[]) =&gt; {<\/span>\r\n<span class=\"pl-mi1\"><span class=\"pl-mi1\">+<\/span> const check = &lt;Type extends Competitor&gt; (data: Type[]) =&gt; {<\/span>\r\n      return data.map(competitor =&gt; {\r\n          if (competitor.weight &gt; 2121.5) throw new Error(\"Stop the show, world record hit!\")\r\n          return { ...competitor, judge: (...args: unknown[]) =&gt; { } }\r\n      })\r\n  }<\/pre>\n<\/div>\n<p>This is testing a few different things:<\/p>\n<ul>\n<li>Writing generics with an arrow function<\/li>\n<li>Using an <code>extends<\/code> constraint for the interface subtypes<\/li>\n<li>Re-using the type parameter inside the array<\/li>\n<\/ul>\n<p>We left a tricky problem with this challenge, but explicitly didn&#8217;t call it out. The function <code>judge:  (...args: unknown[])<\/code> is a types gap. There is no validating that the judge function actually works like expected. There are two approaches for handling this:<\/p>\n<div class=\"highlight highlight-source-diff\">\n<pre><span class=\"pl-md\"><span class=\"pl-md\">-<\/span> return { ...competitor, judge: (...args: unknown[]) =&gt; { } }<\/span>\r\n<span class=\"pl-mi1\"><span class=\"pl-mi1\">+<\/span> return { ...competitor, judge: (...args: Array&lt;T[keyof T]&gt;) =&gt; { } }<\/span><\/pre>\n<\/div>\n<p>This version <a href=\"https:\/\/twitter.com\/faridz974\/status\/1454107977267417089\" rel=\"nofollow\">from @faridz974<\/a> would ensure that the right values were used in the function (e.g. you couldn&#8217;t accidentally put in an object to something which could only accept <code>string<\/code> and <code>number<\/code>s) but it ignored the order. An accurate, but whole-heartedly not recommended for production version which does take order into account comes from converting an <a href=\"https:\/\/github.com\/Microsoft\/TypeScript\/issues\/13298#issuecomment-707369176\" data-hovercard-type=\"issue\" data-hovercard-url=\"\/microsoft\/TypeScript\/issues\/13298\/hovercard\">interface to a tuple on GitHub<\/a> which is a bit too long to print in here, but here&#8217;s a <a href=\"https:\/\/www.typescriptlang.org\/play?#code\/PTAEE0HsFcHICcCmoCWA7UBjAFgQ3gObKQBmo80aa6BoADtALZ0DW65uALjVpM4t26Q0AZwA0oAJ4xQeAG6IAUCFC5QJRAHdQnSOUqq0AEykxYC0ACtoIzqAKQ7nbMhEoAXskx86AlEIxNSHg2NAIAOkVFdE5EeBJcTGQAYR8-XXhQAG9FUDzQYAAqQtAASQwAGwAjEVBC4Fz8zUQUAmxOAC5QNCYquIBuRQBfKJi4hKTQAGUPRFT+QRRhUEQAD1jjWvnfQWDs0BHotFj4xOQAEURveC4UBW30pYw1jaMttN3MnPydF0ZELq2eA0YZRFQAFWwKFq3jQCngnFquAw+Cq-hu8EkoEgVUsV04AFosZxJL5UMc9GpONA6BVEOFQMowO1OHQRB0QAR-NhoFV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Q7DHm5uF1yOM9Azec8Pif84yQuF1LktFCv4+09Pqua-gOucF7jv1pg1uI5907t3RAP9PgDyOhIdu4Cez-AkHaIeiggA\" rel=\"nofollow\">working implementation in the playground<\/a>.<\/p>\n<p><a href=\"https:\/\/www.typescriptlang.org\/play?&amp;q=2#code\/PTAEE0HsFcHICcCmoCWA7UBjAFgQ3gObKQBmo80aa6BoADtALZ0DW65uALjVpM4t26Q0AZwA0oAJ4xQeAG6IAUCFC5QJRAHdQnSOUqq0AEykxYC0ACtoIzqAKQ7nbMhEoAXskx86AlEIxNSHg2NAIAOkVFdE5EeBJcTGQAYR8-XXhQAG9FUDzQYAAqQtAASQwAGwAjEVBC4Fz8zUQUAmxOAC5QNCYquIBuRQBfKJi4hKTQAGUPRFT+QRRhUEQAD1jjWvnfQWDs0BHotFj4xOQAEURveC4UBW30pYw1jaMttN3MnPydF0ZELq2eA0YZRFRQaCZbxGZBBEK1AhILigTQuDDSaAouLENAVSSyXDGCo8bwLfxPETKMC4KowJwuUBuTygUiqehMVjsKrQOyoxDomS2fCxEy6AlEry4ESIWrElhKFTOZAw663Zaknbk4S1AC0pkxJBgxl+yOcyIxoCMKCMaFgdnkyCpv2QNJQxM4+LFiSSIlqIj4xDISpW6xu6hQiAqb3URpMy2DTsuqu49w+WrQAEJQMlCfqQ69naAAAY4K4sIvqSiYAKgPoVSCaJ1ipCMSAWYMat1xFbweB7OsNgD8oCi3lEdlLmBYoAAvKAADwAFUkvnz-OjD0+AD5QAAKIxcXBdZe+ADaAF0AJSznfffJITiQjAHzi4cKMXB0Xca9J7Ge3xofjyFAyG-NMMnCZpWnaUAdwAJgARkQ8IAFZr2cPttDQLRQAAUV7YJdwAIimXQ6ELERsAbCQ4SjcgrmCExsH8DMiMvQCgIfJ99nCXif0+CRrCMIgul3XjwnwAgRC6SgWDQBs0Ava9-32IYDg4oZ2MOMdbEZWZN38YJajnU8sixaDOlARCAE4DgkMyoLaSykIABjs-ZHPaLoEIAZgAdgANnchyWicro4J8g5z0UHS7BVYJblTMkMmM0BTOdf4uiI1s5AjEQiJo0KvNAALIqGeyMoBUAiLoYJOBufwCvMsLQAAFlaqKwTARcGSZLxwKeFFghYBEkU4UdtTsPqDJrOdJxYXdpvAoz2JUfIAD1B0UJbkqecJDXgXDEmwMDkr-ACfn4wz4HCISiFOzUIM8zgtPYp0euVBibhTfrduWFARFtOw6ClNwwkLD1fB1ebfhgNoJvHS0vsSuYBuWOaXCnfdkZ+maVqdDatsUeLvruVG-rQfbgiOnAHt-TIVLvPIrogu7EDpz5IKKzgJBZ4JwiVf5XsUIA\" rel=\"nofollow\">Our answer<\/a>.<\/p>\n<h2>The Challenge<\/h2>\n<h3>Beginner\/Learner Challenge<\/h3>\n<p><a href=\"https:\/\/gist.github.com\/gists\/927ccc66ae3022dc64c4f650109b937a\" rel=\"nofollow\">Update your website&#8217;s color scheme for Halloween and tidy the codebase up a bit<\/a>.<\/p>\n<h3>Intermediate\/Advanced Challenge<\/h3>\n<p><a href=\"https:\/\/gist.github.com\/gists\/927ccc66ae3022dc64c4f650109b937a\" rel=\"nofollow\">Handle a book stores halloween discount event<\/a><\/p>\n<h2>How To Share Your Solution<\/h2>\n<p>Once you feel you have completed the challenge, you will need to select the <strong>Share<\/strong> button in the playground. This will automatically copy a playground URL to your clipboard.<\/p>\n<p>Then either:<\/p>\n<ul>\n<li>Go to Twitter, and create a tweet about the challenge, add the link to your code and mention the <a href=\"https:\/\/twitter.com\/typescript\" rel=\"nofollow\">@TypeScript<\/a> Twitter account with the hashtag <a href=\"https:\/\/twitter.com\/search?q=%23TypeOrTreat\" rel=\"nofollow\">#TypeOrTreat<\/a>.<\/li>\n<li>Leave us a comment with your feedback on the <a href=\"https:\/\/dev.to\/typescript\" rel=\"nofollow\">dev.to<\/a> post, or in this post.<\/li>\n<\/ul>\n<h3>Best Resources for Additional Help<\/h3>\n<p>If you need additional help you can utilize the following:<\/p>\n<ul>\n<li><a href=\"https:\/\/www.typescriptlang.org\/docs\/handbook\/intro.html\" rel=\"nofollow\">The New TypeScript Handbook<\/a><\/li>\n<li>The <a href=\"https:\/\/discord.com\/invite\/typescript\" rel=\"nofollow\">TypeScript Community Discord<\/a><\/li>\n<li>The comments on each <a href=\"https:\/\/dev.to\/typescript\" rel=\"nofollow\">Dev.to<\/a> post!<\/li>\n<li>Our previous <a href=\"https:\/\/www.typescriptlang.org\/play\/#gist\/303ebff59a6fc37f88c86e86dbdeb0e8-0\" rel=\"nofollow\"><code>Type | Treat<\/code> 2020 challenges<\/a><\/li>\n<\/ul>\n<p>Happy Typing \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Type | Treat Challenge 5 Welcome to the fifth, and last, Type | Treat challenge! These challenges are a series of blog posts which have 2 code challenges in, one for beginners and one for intermediate TypeScript programmers. We&#8217;re on day five, which means going over the answers from yesterday and have 2 new challenges. [&hellip;]<\/p>\n","protected":false},"author":6925,"featured_media":3143,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3172","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-typescript"],"acf":[],"blog_post_summary":"<p>Type | Treat Challenge 5 Welcome to the fifth, and last, Type | Treat challenge! These challenges are a series of blog posts which have 2 code challenges in, one for beginners and one for intermediate TypeScript programmers. We&#8217;re on day five, which means going over the answers from yesterday and have 2 new challenges. [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/posts\/3172","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/users\/6925"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/comments?post=3172"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/posts\/3172\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/media\/3143"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/media?parent=3172"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/categories?post=3172"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/typescript\/wp-json\/wp\/v2\/tags?post=3172"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}