Type | Treat Challenge 2
Tags: typescript, javascript, codenewbie, challenge
Published: 2020-10-27 22:56:59

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](https://www.typescriptlang.org/docs/handbook/2/indexed-access-types.html) 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.

```diff
// Your goal: remove this any, without changing GhostAPIResponse
- const displayHauntings = (haunting: any) => {
+ const displayHauntings = (haunting: GhostAPIResponse["hauntings"][number]) => {
  console.log(` - Title: ${haunting.title}`)
```

[Link](https://www.typescriptlang.org/play?#code/PTAEE0HsFcHICcCmoC2BDA1gSwHYHNQ0dCAHE0AdwAssBjK0WyHHRWgFwGdR3JDQAJmnZoARmk6IAUCFCQAZqAA2kWmiWg8VSJy6EcAnlURZ4oeWgBuMeFnbIqaaDi4A6KTLBRohJIyTCuATsxoxKWIgulDT0jMysHNy8RsKCwmISiAA0nvqGAJ4woI6WyESgAIIACgCSoEicJMySmpGI8MKIhvKQZoU+Trzo7HTqSvnuuQDqxsT9oLpo8Ow88PlBPHwCWI1KaPlGDk4uQZxZoPMB4QBe0rIhqfMCzLArGDiQFMWfmzz5JMgQsgSEs0ChEPYzERDAAraC6UDw5AAAyI+WRk1kqJw6OKElAdm4JD2tGyoFE0BWaDiSiUiDQeGgyCQTBQ4IMXRSK3mahwuSRoFgNQ5AA9OQAVf6ITiwX5IFCQUqHQVo2UUOzaSmMRz4DZA3IAcW0umqNQAStKmjgWuwpZNbQDQEadOxTRbGs1kABeUAAbykoFAODBiAAXAt2LZ8AHyaYQgARToAfnDuijeBjAnpCeTqcjQRjoiwkDz6Y8gcczhG+E44Yq8A6+QAPL6eHY6aWgucSPBFZEiKTO-hzio1CNmEOCABfAB8UinHiY1pW212+2dCJ9AAotC7wxvXbV3VbJABKUBemd+mNLziQOmuFR4LfIgByIfDABJfbvdK5g+CU7IqeMZYIoO7GuwrhFssVCJvY563veiCPpAz7IgAQnGVBCPkX4-pB0HYfBiBASBgZgaAEEuq4WbCHBnSIc0yGoeh8YRAI+G-lBdE5vYZE3sxD5Pi+AA6OBYSWoDftxRGQAJgnWixInIuJ9bsBQvQYLWwExrJlYnDWrg9PAACiaD0FuSwaVpF5Xv6gaBquxL7AAEsc1Z4JwVnLJp8AYORoBTiBC65N4Zh4JA6jhvKfZGDs+j5Oc6ohDAKz0EQeAbAebqWp6Ui3iuOwufk7lVqcF5UQZnn7pBuUetaiAANoAETVacLUALpNTg0AoKI7Sdeel7XoGSHCWhL6gAAtKA4rtmG0m+u1+CuCM7B0gJY1CShKmOd+K14KhY7FjgW1xEpE3ofty0eUErg9n26A4KSAmhUAA)
### Intermediate/Advanced Challenge 

```diff
// 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](https://www.typescriptlang.org/docs/handbook/2/conditional-types.html) to narrow the union, however we got a lot of responses using the [`Exclude`](https://www.typescriptlang.org/docs/handbook/utility-types.html#excludetype-excludedunion) utility type to make it a single liner:

```ts
type AllCandies = Exclude<ResultsFromHalloween, { candy: true }>
```

Which is a great solution. [Full link](https://www.typescriptlang.org/play?#code/PTAEE0HsFcCdQNYEsAmBnUALAhgNwKaiz4AucAdvilpALaEDuSJmo2oDmkANoQEbYA5gCgQoAGbRu3UJHFZs0yA3z5yobpEgkANG3LUAnjADkBUCWwI1FzIRJJ6FyKFFhaVwuxT40AY1gkAAcHSHU5NmlZeRZ8WgAuYWESQyDCAGVybD8EACFsWAwAXlAAb1dQUCz6eNAAInSuWBJQAGEuPx5sEkJ8wrrhSuxaGHISWoAWQdA-bANDWpJYaHxhAF8klLTQAHFoWgFpYrKKquH8WrqdrXxDXf3D7jQBys7NWEvBYjU60AAfepBOBBXjPaazeaLZarDbJVKEACCQRBvlAJXK02qF3qSJRYNecxQCws0PWm3hbS0yFR6NOWMurSpSF8LxmhOJSxW0zSc2gJDQUK5sK2GXISBy+EKfTRJ0x50umXF1kKoD6rIhRMFq0qPPIfIFJKF5O2ABUtCwgtg0D0ZRjKvT6mbtJhLdb8KzaEgxhzSZUlkqtWS4dsAApqPxIGS0uU1eph8gR7is-05QOwsRQaAzYjdLygNBewS8UBBSOECKKGSxIi+KT8vSEjhzFokFzQNCrMQsJAYVv5nDEWR82yML1i8iCDDDMKCUDGOCIVBoAB0rhFoAASrXuPyAGKwOgACUrylU6hKmWyeQKGABewOldvoFxoP+oEVEqlBTfjMg1KfToWlaNoAvGiZJBmpiDuISCFC0giQIWzhVKo1B9h2lqwLmQ4tnYbLzBIB60COJL4N0aA6G4c4wPhoAoC4LDdKAuBIOwnQGMwSBhIoFjwiuwaItIrSEsyxwAKIAB5+Nw0A+AAPFuaB1mg+5HieKhqHopQaj6KxrAAfMahAAJJoCagQ5HJCL6TKCKgPgEk9AYGDlCmCBaqAaygAA-M+oC1JQBCwAA3AJz7SOZSo0qApmRZZinKaptDHkoGnkIZEFgAi3AjNaI7EHofDDtwzAkMWJi5JAfB8HcJrYHwoImPh5DaAo5i6vqq7CeQVHzqAHjWGwGg9i0EQaqJoAAFbti04iQPAmCON5YXHmgYa8vyVk2SUdkOU56AnB1-IeV5vl2QF+BBaF67Zdw3UoKJADqzBcHy616vyMqre9+pWUJIm+PpQA)
## 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](https://www.typescriptlang.org/play?#code/PTAEE0HsFdQCwIYDcCmoGlQJwJ6gM4AOKAxgJYIA2oh0AtoQNZkB2ANOiwCag4ygB3BCwAuoEZFAksKBCLQYROYqABmWSHVBkRAOgBQ+kKAAqWBFx1lILKpTxFZzFgHMOfWCRiUeXFKtYFcWU0SCxtURQsVQQSFCMwVTDxODR8RGIOACNoMQ90GVAbe1AXSFYXcUlEVBSyfCKWNFoGZzYEgkl8rhsAcjFVFBRqSjJGNB1ehoEwkThQLmhCUZI5CpS0PwCWKxsDfS8WfDEWplZQAF5QAG99UClISjCALlAAIhxhp4F6uDf2+5KYivN5laBYLj-O4EMgALxQIIAsihLPQofcUE0sC4cCCAIwABgAnKAANIAKxoUVAhIJLnRam+URRrxilHwKABMPhAGFESCAKwE3QADlAAA9QAAWYUANgloAAzMKAExSOhQgC+AG5DMYAJJHeQWIqqKQyNaudDBFSjcbPBIdIFoAAK9DOLEuN1AHXuXieWFexywFV9oF0EY6msdxh5wl4-Hy0A5NpQkDNzoaEnNsnk1vwFUoaFGWIA-IZnaA3a1zldhDg9WAAHIocViJYcGQchBYEjzOCQAQJzzx5NBXJkUZKVNVeDCLhFp2pAgIOjNDRZItaH5za2qaAsEgiawsXSgAASg6k8fyrZE5iPGw6MhE4M9lfUmjqDX3h+PNlnFwUDEOY0lXNBnXLA4bGOHM5BQABRcVV2WFBqw9L0AAoAEpLgAPhuaEXzfGh3WcfRo30St0OcAAxDQ6Fog8jxPL1630IA), 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](https://www.typescriptlang.org/play?ssl=49&ssc=69&pln=49&pc=73#code/PTAEEkBcHIGdQEYFMkDtQEcCuBLSTQBDdJANzR1QHMAaUWAewFskHUCAbJSUBrHwhw4BYAFAhQkABYEqUhrEjwAnn2jlEKdAGNCkbVM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DYVIbMiZr3HItrvA6NQML2eaeM66RHAe1TJOYKos0S92wI9y7OC9QH800+TmKgvaMb2Eh1eFKqgl4PBGtwhjUBAW2oCqSQEMSnSs2AYQvlwqJcrobLOQgyjOVbBC7T41r4aj6NNE20o4xDsD48jRPPGjRyFDwt7oPyoC477-tEwRbABKR76ft+v4AYB-OMXmcv4RIADqSDqAQHbsFtlTctE-SJKJxv0PIXOoNAPDIJIDApWUmp2NoB6QL2bsYwAor1CnBaF4XgzDfuE3mFXVbV9Xjwl6f4Yx2cSAA8gA0nQ0LwNTCTtiaPq1EkeJXMSjdEqbHpBAsGG6tqiT3PgpjFd9hSaNhgy4uFVyJPqZJcFtM0BADANALCOMfI4TBTj6iQAAfggDAeA2EGAqneCtaYbkIRlW8LwZ0lImg3yYhILG5RuRYE-EEeSoZ0DWAjFGcwMYgA) 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](https://www.typescriptlang.org/docs/handbook/intro.html)
- [TypeScript Discord Page](https://discord.com/invite/typescript)
- The comments on each Dev.to posts!


Happy Typing :)