Type | Treat Challenge 4


Tags: typescript, javascript, codenewbie, challenge
Published: 2020-10-29 20:27:38

# Type | Treat Challenge 4

Welcome to the fourth Type | Treat challenge! Today we will be stopping hauntings and putting puppies in their place. 

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

We were looking for some pretty simple types in this challenge:

```ts
type Treat = { 
  location: string
  result: "treat",
  treat: { candy: string, baked?: string } 
}

type Trick = {
  location: string
  result: "trick",
  trick: string
}

type NoShow = {
  location: string
 result: "no-show"
}
```

We can safely and efficiently reuse the properties in each field by `extend`ing them with an interface.

```ts
type House = {
  location: string
  result: "treat" | "trick" | "no-show"
}

interface Treat extends House { treat: { candy: string, baked?: string } }
interface Trick extends House  { trick: string }
interface NoShow extends House { trick: string }
```

We can specialize each `result` property with the specific value a `Trick`, `Treat`, or `NoShow` will hold by re-declaring it.

```ts
interface Treat extends House { result: "treat", treat: { candy: string, baked?: string } }
interface Trick extends House { result: "trick", trick: string }
interface NoShow extends House { result: "no-show" }
```

Which would give the exact string value for `result` in each type.

[Our answer](https://www.typescriptlang.org/play?#code/PTAEE0HsFcHIDcCmoBGjEDtQGt0AcBLDAc1AENQATAsgJwE9RIAzUemW0AF1oIGNsTTj0RkuAKBChaiAM7QANl1mhmkTu2idZIxFwA03ABaJGxTIlpkFCxs2sLQRLpFCSwXIzOR8xiYuoEcgBc3DJisoY8-NgqZBiUoBiQALSyRpAA7rIAdOLuoADC8WwwoAC2ZLjG3tz0eHKgmUb8RqC+WJRyfLxoTMwFnnLIMvJKsgD8+eJc9cgAEjCyyAC8oADe4qCgCpC+XASQGKE6vCRb0nKKXKEARCJit6AAPqD3vAJPr7fJaRmZt3EAF98s5LPY+MgACrhLigRAADy4mEoKkW0GWG0uYxubweXFuUVhoXW7XilHoJ2iJEMKCqiEoEypZ1IQNAILBtAh0I+gkRyISaKWyFJo2ud2inyJMWZRFZ4k53NAADlIABlf7wpEooUYkXY8VvX7pLJPEHiPhHHRhUTKUIw20AbQAuqA1o6LqTdvtDsc3ujMQBGQkGpQS2Eh-EkskJSlvAAyvtAACE6Gb2fpPTs9mJfXcA8gAMwhsVhvERom26MdCl3ACydBUqdo6aBme2XpzByO+eFoAALCWrmX3rbI8SsXTcJQ7oVIJBsEFZCGa3HbgAldDLWDL9ns8TO-KWjDWyWxe28l1u0AejvZn09-19gBMQ5x4Zi45lbwbxH4ZvbLFvVzR9bgLUAAHY30Nd5P2lAQ7jVLQ8F4ZZKDNA8jytOFkg1LJZFCVU8MyK93SzYDuz9MC+wANmgkdjX+DDDwKVVMmMMRSmgUAjDIJBjGQWYGkiGMuIqekBPKApkjhShoDwBR+D8UBYAo31YHIBIVNLLgNJQRgMTlNwpAoaAMCTNROFgHSNPJcgkkQdjOWWPhKLqBopiAA).

### Intermediate/Advanced Challenge 

This one either stumped people for ten minutes or was considered a breeze. We had a few different answers in the TypeScript team, but we think this one is the most elegant.

```ts
type Result = {
    done: boolean,
    who: string,
    loot: Record<string, any>
}

type TrunkOrTreatResults = Record<typeof trunkOrTreatSpots[number], Result>
```

This challenge was a great reminder that [a `Record`](https://www.typescriptlang.org/docs/handbook/utility-types.html#recordkeystype) is simply a type-alias to a conditional type:

```ts
type Record<K extends string | number | symbol, T> = { [P in K]: T; }
```

Which can convert the original answer:

```ts
type Result = {
    done: boolean,
    who: string,
    loot: Record<string, any>
}

type ResultMapper<P extends readonly string[]> = {
    [K in P[number]]: Result
};

type TrunkOrTreatResults = ResultMapper<typeof trunkOrTreatSpots>;
```

into code which is both easier to read, and requires knowing less language concepts. Kudos to the folks who made it seem very easy! 

Here's [our answer](https://www.typescriptlang.org/play?ts=4.1.0-beta#code/PTAEBEEMDsHMBpQGEDyA1AkuAjATlABaQDOoATgKaQA21AnqAA4CuALqJKAEYCW0AJqAD20UKwIVQ0SK2aVhAMzFkeAYwDWwssqqs+sAHQAoEKACaQud2pCNhEsMasAtBUGshoPsVZVB-T05WMmZoTSFtYN0vaB8-YxMwDFFOEOICMU9GSkZIeQUIsQlQCgA3CmhWRDpLezJ+VSF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JXKSLy8USfaBhsThWYUDpYC6pY1CCHF6hGElQgpZqUEUDqeqMFl4WDuwjJCYVoAAGKQDw1CJe5KUKkqEoAMzNbljKcf1BWdbSfX1c40yZPql7SNQYg2jM8icAoJmgNQfAdNwFA2KZ4jTLKxLXTdnXmvk0D6B283DmQvSddgBiehMpCXZwAJAq06akDkQhcMOfy3X0KpfWl8h-aArlkL1VQ-GZAPAkMHr5LAzB-KCUOmKNX16VIFCtNsfAtLGrSLuozCMFkpTxsjF5mRQsaJqAG6wEAA).

## The Challenge
### Beginner/Learner Challenge

Don't freak out... Ok freak out a little bit cause WE ARE BEING HAUNTED! And **they are after our code**! We have tried everything but we cant seem to get it right!

Somehow they keep manipulating the objects set this [snippet of code](https://www.typescriptlang.org/play?#code/PTAEDkHsFpIOwKagNZ0gdwM6nQCwIYAuoAltgOaQlzmjwA0oARgK7ECekLohu1yoTiwBOOSMOQAHADb4AxkgBQIUtgIs4hBABNm7UPlCTI0rcPIIyhAHSgAksWTVthzCznJM15WB+gAmlyikEwAVghyhNjICAiSoBaEhNS0cgQ0OswIfHC6vEiYCoiY9H6YhJDxvGSgAGbCkAC2oASSkghwKXqgjfhONIJBdGERUaDCCPja8NLsfhWg5ZU8fNiYxpDI+kzZ+ABuJOLeioTs7aAASqPi+gC8oADeiqDjkE2YAFyXb40A2gC6z1AaDICE+Bjg7ABigAvooTmckBcfqB7k8XnB8I0EF9ysIUkDpuJwXAWI0dsIgehnBgSWSKUDyLhIOUAPxffCQ2Hwvy4JKSD4gdDC6waBAADxk+GoOnKLFqtWsciawAA4szyphoJztNAAArpCqNTDAABiWK4WoAEvgNFpdVbLWDtbl9bIFFqAKKSdDiXjQCaRG7WPmNaSKZVwcqgOK+4S8K5B4R3R5AlR8wiST4gWN+5nSbSBirJ6zicii5DAXPx3CwAsB67JuvaYAHTAkQitkjxlj4aTQCoiYBAhrvL6-IEvB7ArE40AAIk9mmEnIUoBt0mk88YROE4IArIxqblaV8AEygGGlF5TmfYr7zgAquCQAGkO2kOtvQLvwWejzSWBfAAzJe143tOmL3guer4MI0hBN+v7ngBJ5AaAoFXpOjx3nOT4vhAIiYAgyZIW8e4oTggHgph4G3lBeHPkgACCSQkHIZHEpRx7TOhACMYHYZBs4PkxoAAMqECw2jsJxFGgP+VFoX+gk3jhDEPgAsvg5QkaAABCOijo0cngnxqG8eCAAsqkQbhD6GYWKJnqZ3HUSBl5AoCcLwioz41Mq2gFMyLAFqAci2sR4VNJIJDSHpJENHuii1BokSHHALS2poAAURY3F8ibFuwACUqYvJG0bGai4yNuw1jGZgvzabw1i1Ah4g5S1uANTqTQ5WVABUtVJvVjXWPFNC8CV-wANwjj81hMiyxBovZC4APJhQAUggCoTLJnlwkAA). Take a look and see if you can force the ghosts to stop moving things around.

### Intermediate/Advanced Challenge  

You've been roped into helping out the halloween puppy parade, it was such a distracting even that you did the minimum possible to spend as much time watching the show.

Now it's over, you figure it's time to clean up your code and try get [it type-safe](https://www.typescriptlang.org/play?ts=4.1.0-beta#code/PTAEE0HsFdQc0gF1AJ0gBwKYBNQEsA7RSURAC01AGNpFMBnZMgQwBtXIB3TTA6yALZZEeEZAIAaUACto2OITgAoEKGyQ4cAJ6gW7Ljz7D+jaAIahmycpWYEC0NqHTMUzbJgB0oJUo9VWV0oAM2gCKjE+fzwPAHVCAkwUAAoAIxQebAAuUEYURSkqSFNzHLzFAEocgG9QAmZS3MR8gjgpADcYzEgy5sVQAF8lTkJ1Tk9ouISk0ABeXMFuxIBRVnpMABFMKi76X1UAeTIpZgjHdh0tGDUYggByZABaBfNxSkw1ygRrEnVSClAoT4qVEnjBKjAABUyDA4GRQFdYFQ7KBOFYqPDRKBxKAAF6QQQ5MiIRDoLIgTiUzzEIQAa0I9HoAEdHBl1HAUGFPEUBMA9BxuLx9mAAJIERiYdxSRF3dqUVKGSz0Wk4UgkCisdCkNisHTQLXMXJFDKpSCuXCQYL-SgCYrICHODDQQIoEyIMwWU7G7D9YjW0C2xiO9DO1ygdKZejeKDQWXy6B4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CHcA6olbNmREIMjCuBEDqHQq8aL5XyvQbgKB9571UAAWTPAQXUMhoBBncD6SC6YExZhzHDOBhZIhSDAv8d8NDkR8DBv6fckBDzMFNI+RCWs0zODQF7AQO8zKILIUsTAqx1hbB2B4JyTVMi9BaHlAaEkXGVBqHUBomBPGtA6F0HoTRXGDDau1IGDAThMxsOsNQGg6wsEfIaduqNOaYjrLIERT4oYHi8GCZmZAjo1TgLwVKdgKLQxKskegeBcR+LqDpJI0VZjFRIsbAAgigNwWgal1MwBUVu7Z0DJAOnMYqfDyCeGCETFIkyyCeG4uoAQYyABUoBvwADYKjrU7n0VoyQtkVB2dIReBBkh3DuBUAA3EdL60AfoxSOqNVJORSkEHKQQSpUNkjbLNqNUaM9uhvLKRU6GvyXrtSGAMIAA).

## Submissions

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