Type | Treat Challenge 5


Tags: typescript, javascript, codenewbie, challenge
Published: 2020-10-30 21:06:05

Welcome to the final Type | Treat challenge! Today we will be restocking Halloween candy and identifying Halloween movies


## Yesterday's Solution
### Beginner/Learner Challenge

We were looking for using the [`Readonly`](https://www.typescriptlang.org/docs/handbook/utility-types.html#readonlytype) utility type to force the type to not allow poltergeists to make changes to your rooms.

```diff
- type Rectory = {
+ type Rectory = Readonly<{
    rooms: Room[]
    noises: any[]
- }
+ }>

- type Room = {
+ type Room = Readonly<{
    name: string
    doors: number
    windows: number
    ghost?: any
- }
+ }>
```

It's worth remembering that JavaScript doesn't have immutability like this, so this really only affects the type system. You'll get a compiler error, but people can still work around that.

[Our answer](https://www.typescriptlang.org/play?#code/PTAEDkHsFpIOwKagNZ0gdwM6nQCwIYAuoAltgOaQlzmjwA0oARgK7ECekLohu1yoTiwBOOSMOQAHADb4AxkgBQIUtgIs4hBABNm7UPlCTI0rcPIIyhAHSgAksWTVthzCznJM15WB+gAmlyikEwAVghyhNjICAiSoBaEhNS0cgQ0OswIfHC6vEiYCoiY9H6YhJDxvGSgAGbCkAC2oASSkghwKXqgjfhONIJBdGERUaDCCPja8NLsfhWg5ZU8fNiYxpDI+kzZ+ABuJOLeioTs7aAASqPi+gC8l5PTcLMAPADeiqDjkE2YAFyXH6NADaAF1PqA0GQEP8DHB2GDFABfAB8ihOZyQFyBoHuVymM3Y7whcHwjQQAPKwhSEOm4lhcBYjR2wgh6GcGAZTJZEPIuEg5QA-AD8PDkWj0SpcElJH8QOgFdYNAgAB4yfDUHTlFi1WrWORNYAAcX55Uw0FF2mgAAV0hVGphgAAxMlcc0ACXwGi0VvdbphFtyNtkCnNAFFJOhxLxoBNIjdrNLGtJFAa4OVQHEo8JeFd48I7qAPl8pTL-iAs9H+dJtHGKgXrOJyErkMBKzncLAa7HrgWu9pgAdMCRCIOSDmWPhpNAKiJgBCGr8AcCIV83pCyRTQAAiMOaYSihSgT3SaTbxh04SwgCsjHZuU5AIATKAkaUvmuN+SAduACq4JAAGkRzSDpz1AS9YSfO8OSwAEAGZX3fD911Jb8d2tfBhGkIJwMg58YIfODQEQt9VyLL8tz-ACIBETAEALPCfivAicFg2FSOQz80Ko-8kAAQSSEg5CY+lWPvaZiIARiQ8jUM3H8+NAABlQgWG0dhRJY0BoLYoioNkj8KJ4n8AFl8HKBjQAAIR0RdGi02EpMIyTYQAFkMlDKJ-WzaxxJ9HPE9iENfCFwSRdE-H-GoDW0Ap+RYGtQDkL16OSppJBIaQrIYhor0UWoNEiQ44BaL1NAACjrG4ATzet2AASiLCE0wzezcXGXt2GsezMGBczeGsWocPECqBtwHrLSaCqmoAKk6-Nut66xspoXgGtBABuBcgWsPkBWIe55PQ7cAHkkoAKQQXUJk00KIqAA)

### Intermediate/Advanced Challenge 

This challenge evolved quite naturally from the third challenge with the addition of 4.1's heading feature Template Literals.

The most elegant answer moved the return type into the object `const winners` which would then be inferred as the return type of `tallyPopularWinners`:

```diff
const breeds = ["Hound" , "Corgi" , "Pomeranian"] as const
const costumes = ["Pumpkin" , "Hot Dog" , "Bumble Bee"] as const

+ type Breed = typeof breeds[number]
+ type Costume = typeof costumes[number]
+ type BreedCostumeCombination = `${lowercase typeof breeds[number]}-${lowercase typeof costumes[number]}`

function tallyPopularWinners(_breeds: typeof breeds, _costumes: typeof costumes) {
-  const winners = {} as any
+  const winners: Record<BreedCostumeCombination, ReturnType<typeof decideWinner>> = {} as any
```

[Our answer](https://www.typescriptlang.org/play?ts=4.1.0-beta#code/PTAEE0HsFdQc0gF1AJ0gBwKYBNQEsA7RSURAC01AGNpFMBnZMgQwBtXIB3TTA6yALZZEeEZAIAaUACto2OITgAoEKGyQ4cAJ6gW7Ljz7D+jaAIahmycpWYEC0NqHTMUzbJgB0oJUo9VWV0oAM2gCKjE+fzwPAHVCAkwUAAoAIxQebAAuUEYURSkqSFNzHLzFAEocgG9QAmZS3MR8gjgpADcYzEgy5sVQAF8lTkJ1Tk9ouISk0ABeXMFuxIBRVnpMABFM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8BVctaAdH2MnoZ1kBGdhdGYR8VS0BDoIxSwMlASrqtwaHMAnHQBDwC3Efh1U7FwXGpC8OAsm7DbVFJsiYTCbBHhDOlCDImowU8AYJAdZvW5ox4YWQdke-WvuB6HjGW+o9uKzcvSFUwKfqhnwem-n40FA72T5I3me562vuHXVuGdf2-zut1XrQxQWX9vlyNFYk1G8PEMC808Dg4GSNfT+Dp2Ls1rMMaY+1yIj3bp3OS3c+KeHqOYbsABlVurA85nlYBAvazkyJwPkgfLuClEGdA8JANB6BICJiSHgYImZSCYTrKWLg9dfAcM4Rw1Q34cjQlHOGBMmCxzBCtF0bmwQ0ACF9joAAzFIF2MFBGJicmXdAGROgwDrNgZgqc1SUK4aoAATHwgEeBcz5l4KeSIqJExzgyBmLOKIUrsxStiHcnASBsHZuHOOoh4RgRQTvVErZsyIiNkYVwIgdQ6AYTRSwfA7D0G4CgLhnDVAAFkzwEF1DIaAQZ3A+kgumBMWYcw20sYWB6Cx-jvnCciPgJt-T7kgIeZgppHyIVbmmZwaBdL5nYe1GxgSliYFWOsLYOwPDDWRpkXoLQ2igAJhJeZlQah1AaJgVZrQOhdB6E0BZgx0YYwNgwE4CcbC5XZHWFgj5DTIOdunTEdZZAFKfBbA8XgwSJzICrOGcBeCHTsBRS2-lkj0DwLiLZdQmprVmEDEifcACCKA3BaHBZCzAFREHtnQMkTmcwgZZPIJ4YIAcUjErIJ4bi6gBAEoAFSgG-AANgqAzVBfRWjJBZRUNl0gaEEGSHcO4FQADcKtNbQBeutFWpMHk5ABQQIFBAQUW2SKywepNSbkO6AqwFwLLbquVqTIYAwgA) Also, we watched the full two hours of that video, and it's a great background video while you're working.

## The Challenge
### Beginner/Learner Challenge

You're in charge of restocking the houses on your street, can you find a way to reduce the duplication in your notes by generically declaring a house?

[Help keep those houses DRY](https://www.typescriptlang.org/play?#code/PTAEHUFNQCwQwG7TqAzpALqA9gM1tgK7qo4B2oKqGATpJqAO4wCWAxjAFAigDmLSUkSxs4ZACYBPShJxtRqFtjJwANqull640nQC01bGwDWLMrwB0nbmAAqrUi1JjK8wjTgZodAA510ZBieShR4oBgw0DBEJDagAEaEWBGQdADkzqCq2Fhh4oQ+quyekOKgbNji0GawqZAANDJlcZJETGLJ2OV0JZThkj7QzOww5USqZb7+kIGgcWLS0cSQVnEAkljZ2MakRcbQKeQa4azmTKwc5fDmkKTxmIz0ZHFLJKBOJ96Y7hQIaoTQMIAA1o7GMAHkaLYehggU04odcCwaNRQD44B4ALaYVI4fBA-wYIzGIEWOZxADCLlahFAyz6vBmqXY-UG4S6mLg+z6QIAEjFIHCMANoHFhpd4JlFOZVMgaLxCNjZrhsDRPqA6BgfqA-qoAXMeMDQSZIdDIJ44WIyod0Vj2aACbciSYgQB+azCtkAMWR1H59IAvKAAN6cUCgcTYVUAOUV9xoAC5QABGADcYfCNDBpphAAoAJRJgBE8SjxiLoAAPqAi6IJJIi+nw4TibmWF5Mahi6XthXq7WrQ3CzrsCxxOmAL7pzie6AAZUgFQk-vQoCDofDkZjcdSSYATE3M9moXnh0WiTkYPEaMQYH2a5izBhUI2My2TG2O12axeItfb-eRaPoEL7Dggo7jpwU4eiKoD2Mi4grtA64ZluNCxpi8ZJgAzIexoQie5oYAWxZ1lIgFwD4hSQIBkZkEkL6Hu+xifpAnakYOFFUbKtHKAxRZgRBk7TrOoBekQNAREha4hqhUboTuiagAALHhWYmoRngkTWZENlWNaoD4ZbSIZY6pIxb5Oq27Zsd+A71oBhnGWgPhmSiAlJuBY7CTBbJzrQ9BYEGADaPoohgSGNAuS6IQKjTwTQsXLI04nuFJAoALrpkAA)

### Intermediate/Advanced Challenge  

You're working on a horror movie night. You've skipped the types because you figured it'd be simple to work with but someone put the wrong movie in and The Nightmare Before Christmas is not a halloween movie. _It's in the name_. Anyway. To avoid this happening again, you figure it's time to add types to the function. Once, you got that down then you wonder if you can do the same thing for the kids schedule?

[Help type the scheduler](https://www.typescriptlang.org/play?ts=4.1.0-beta#code/PTAEE0HsFcHICcCmoAOBDeAXUk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oEgJYPOkzTCUVKNYTU5Lek0vdKQCVxLTAAGY-TLw7HUFQoqkgOVrHUNA9AtVlQLkwcxjw0DsEUeWDVvgRW2DFROFYndFVRhjK7FQRggA)

## Share

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 :)