Type | Treat 2021 – Day 2

Type | Treat Challenge 2

Welcome to the second 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’re on day two, which me…


This content originally appeared on DEV Community and was authored by Orta

Type | Treat Challenge 2

Welcome to the second 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're on day two, which means going over the answers from yesterday and 2 new challenges.

Yesterday's Solution

Beginner/Learner Challenge

The first part of the solution for this challenge used as const to trigger "Literal Inference" - basically telling TypeScript "Don't convert the array to string[] but consider it a constant set of string literals. This meant that playlist[0] stopped returning string and started returning "The Legend of Sleepy Hollow by The Monotones.mp3".

  const playlist = [
      "The Legend of Sleepy Hollow by The Monotones.mp3",
      ...
- ]
+ ] as const

The second part of the challenge used typeof types to extract the type from the playlist array. Without the first change, this would be string but after the change this meant the full array of different types. You then needed to use the type index syntax [number] to declare that you want any potential string from that array.

- function playSong(song: string) {
+ function playSong(song: typeof playlist[number]) {
      api.play(song)
  }

Successfully completing this challenge would raise an error in the final code samples due to a subtle typo.

Our answer.

Intermediate/Advanced Challenge

This pattern is quite common in code we write in TypeScript codebases, you create one function which takes the result of another and keeps passing objects between functions in a pipeline. One of the best techniques for simplifying this design pattern is to use ReturnType with typeof myFunc to map the return type of one function to the paramter of another. This removes the need for intermediary types which need to be updated when the functions change.

  const findOldCostume = () => {
      // ...
      return { jumpSuit, estimate }
  }

- const createNewMask = (costume: any) => {
+ const createNewMask = (costume: ReturnType<typeof findOldCostume>) => {
      // ...
      return { ...costume, mask }
  }

The little extra step at the end was a small reminder that you can use this technique to provide a type which can be re-used everywhere.

type Costume = ReturnType<typeof assembleCostume>

Our Answer.

The Challenge

Beginner/Learner Challenge

Help sort out a large array of pumpkins, and then get cooking soup.

Intermediate/Advanced Challenge

Fresh back from robotics camp, can you make the perfect punch mixer bot?

How To Share Your Solution

Once you feel you have completed the challenge, you will need to select the Share button in the playground. This will automatically copy a playground URL to your clipboard.

Then either:

  • Go to Twitter, and create a tweet about the challenge, add the link to your code and mention the @TypeScript Twitter account with the hashtag #TypeOrTreat.

  • Leave us a comment with your feedback on the dev.to post, or in this post.

Best Resources for Additional Help

If you need additional help you can utilize the following:

Happy Typing :)


This content originally appeared on DEV Community and was authored by Orta


Print Share Comment Cite Upload Translate Updates
APA

Orta | Sciencx (2021-10-26T19:35:06+00:00) Type | Treat 2021 – Day 2. Retrieved from https://www.scien.cx/2021/10/26/type-treat-2021-day-2/

MLA
" » Type | Treat 2021 – Day 2." Orta | Sciencx - Tuesday October 26, 2021, https://www.scien.cx/2021/10/26/type-treat-2021-day-2/
HARVARD
Orta | Sciencx Tuesday October 26, 2021 » Type | Treat 2021 – Day 2., viewed ,<https://www.scien.cx/2021/10/26/type-treat-2021-day-2/>
VANCOUVER
Orta | Sciencx - » Type | Treat 2021 – Day 2. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/10/26/type-treat-2021-day-2/
CHICAGO
" » Type | Treat 2021 – Day 2." Orta | Sciencx - Accessed . https://www.scien.cx/2021/10/26/type-treat-2021-day-2/
IEEE
" » Type | Treat 2021 – Day 2." Orta | Sciencx [Online]. Available: https://www.scien.cx/2021/10/26/type-treat-2021-day-2/. [Accessed: ]
rf:citation
» Type | Treat 2021 – Day 2 | Orta | Sciencx | https://www.scien.cx/2021/10/26/type-treat-2021-day-2/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.