Welcome to the second Type | Treat challenge! Today we will be deriving the types of pumpkins and busting ghosts!
Yesterday's Solution
Beginner/Learner Challenge
The solution for this challenge used indexed types to extract a part of an existing type to avoid duplication. The need to use [number]
is an interesting twist, because [0]
or [1]
(or any number) would have worked just as well too. Doing this right would also have raised the typo in the original code.
// Your goal: remove this any, without changing GhostAPIResponse
- const displayHauntings = (haunting: any) => {
+ const displayHauntings = (haunting: GhostAPIResponse["hauntings"][number]) => {
console.log(` - Title: ${haunting.title}`)
Intermediate/Advanced Challenge
// You're first going to need to separate out the candy from the treats,
// you can do that via conditional types.
- // type AllCandies = ...
+ type IsCandy<A> = A extends { candy: true } ? A : never;
+ type AllCandies = IsCandy<ResultsFromHalloween>
- // type AllTricks = ...
+ type IsTrick<A> = A extends { trick: true } ? A : never;
+ type AllTricks = IsTrick<ResultsFromHalloween>
// Almost there, but little 'Bobby Tables' cannot have peanuts. Can
// you make a list of candies just for him?
- // type AllCandiesWithoutPeanuts = ...
type HasPeanuts<A> = A extends { peanuts: true } ? A : never;
type AllCandiesWithoutPeanuts = HasPeanuts<AllCandies>
Our original answer relied on using Conditional Types to narrow the union, however we got a lot of responses using the Exclude
utility type to make it a single liner:
type AllCandies = Exclude<ResultsFromHalloween, { candy: true }>
Which is a great solution. Full link
The Challenge
Beginner/Learner Challenge
Lets take a trip to the pumpkin patch and try to find the perfect one for our Jack O'Lantern. But in order to make sure we have located the right type of pumpkin, we need your help in identifying pumpkin types.
We created some starter code you can find here, lets see if you can finish it.
Intermediate/Advanced Challenge
Your job busting ghosts just got real tricky. Before you head in to guard Manhattan, you need to assert to those ghosts who is boss. Help finalize the ghost-busting algorithm because who else are you gonna call?
Sharing
Be sure to submit your solution by using the Share button in the TypeScript playground.
Then go to Twitter, and create a tweet about the challenge, add the link to your code and mention the TypeScript page (@typescript)
Need Extra Help?
If you need additional help you can utilize the following:
- The TypeScript Handbook
- TypeScript Discord Page
- The comments on each Dev.to posts!
Happy Typing :)
Top comments (12)
In the advanced/intermediate challenge. It states that
'Bobby Tables' cannot have peanuts
but to me, the solution seems to do the opposite and actually keep all the candies where there are peanuts 🤔I agree with your implementation. I was discussing the solution provided in this post :)
Maybe @gcrev93 can help me if I misinterpreted it
My bad, should have said something instead of just plopping down a solution - yes you are correct saying that:
is the opposite of the solution. Something more correct would have the inverted type. I'm not sure how one would go from
HasPeanuts
to something likeOmitsPeanuts
but declaring it from scratch is straightforward:Also, the proposed solution for AllCandies removes all candies from the union, instead of only keeping the candies.
This should probably use
Extract
instead ofExclude
:Bobby is on his way to the hospital now. Thankfully somebody had an epi-pen ready.
"The need to use [number] is an interesting twist, because [0] or 1 would have worked just as well too. Doing this right would also have raised the typo in the original code."
I'm confused by the wording here. So,
number
AND[0]
or[1]
would work? So, you don't actually have to usenumber
?Yeah, you can use
number
or any literal like1
,2,
etc.In this case,
number
however feels more right, because you're saying any number from the index, not a specific one.Thanks! That makes sense.
Here is my attempt :)
If we aren't allowed to change
type Ghosts
andfunction investigateReport
which accepts an array ofGhosts
, can we simplyreturn false
for bothfunction areGods
andfunction areEctoplasmic
?My implementation of
function areDemons
using user-defined type guard:Not if you want to live... and save NYC.