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 showcasing 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.
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>
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:
- The New TypeScript Handbook
- The TypeScript Community Discord
- The comments on each Dev.to post!
- Our previous
Type | Treat
2020 challenges
Happy Typing 🙂
0 comments