DEV Community

Discussion on: Type | Treat Challenge 2

Collapse
 
dungdung profile image
Houston
type AllCandies = Extract<ResultsFromHalloween, {candy: true}>;
type AllTricks = Extract<ResultsFromHalloween, {trick: true}>;
type AllCandiesWithoutPeanuts = Exclude<AllCandies, {peanuts: true}>;
Enter fullscreen mode Exit fullscreen mode
Collapse
 
johnnhoj profile image
John Cheng • Edited

I agree with your implementation. I was discussing the solution provided in this post :)
Maybe @gcrev93 can help me if I misinterpreted it

Thread Thread
 
dungdung profile image
Houston

My bad, should have said something instead of just plopping down a solution - yes you are correct saying that:

type AllCandiesWithoutPeanuts = HasPeanuts<AllCandies>
Enter fullscreen mode Exit fullscreen mode

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 like OmitsPeanuts but declaring it from scratch is straightforward:

type OmitsPeanuts<A> = A extends { peanuts: true } ? never : A;
Enter fullscreen mode Exit fullscreen mode