TypeScript Exercises Bonus🦠 - Type of Pandemia 
Tags: typescript, javascript
Published: 2020-03-18 11:01:16

--- series: Advanced TypeScript Exercises ---
We are where we are, in the middle of pandemia, this sadly 😢 is the subject number one which will impact our everyday life. I am developer from Poland, Polish authorities have made very restrictive movements, and all restaurants, pubs, even shopping centers have been closed. Those all actions were made in order to block spreading of covid-19. As we all need to live in the current situation, I made a bonus task to the series of **Advanced TypeScript Exercises** bonus task located in the domain of current situation and pandemia. I hope these tasks will make your brain 🧠 busy for some time.

### Let's take out the pandemia and put it into TypeScript types!

The puzzle 🧩 you will face here has little or none application in real software. I am using here TypeScript type system like it would be (and it is) stand alone language, if you want to know more about TSts please take a look at the article - [TypeScript is more than you think](https://dev.to/macsikora/typescript-is-more-than-you-think-2nbf). In this exercise there will be no values, only types and types transformation, so our code will be purely at the type level. Ok, lets jump to the code.

Below is how our patient and sickness state are represented in scope of our exercises 
```ts
type Sick = {state: 'sick'}
type Healthy = {state: 'healthy'}
type Quarantine = {state: 'quarantaine'}

type Patient = {
  name: string
} & (Sick | Healthy | Quarantine);
```
So we have three states of patient condition, now our patients:
```ts
type John = {name: 'John'} & Sick
type Tom = {name: 'Tom'} & Healthy
type Kate = {name: 'Kate'} & Sick
type Mike = {name: 'Mike'} & Quarantine
type Paul = {name: 'Paul'} & Healthy
type Doroty = {name: 'Doroty'} & Quarantine
type Gregory = {name: 'Gregory'} & Sick
type Pierce = {name: 'Pierce'} & Quarantine
```
We have many different patients with different conditions. Exercises will work with these patients.

For some of questions, there will be need to use functions working with tuples. Below utility types/functions which will help in exercises:
```ts
// removes element on the beginning of the tuple
type Shift<T extends Array<any>> = ((...a: T) => any) extends ((a: any, ...result: infer Result) => any) ? Result : never;
// adds element on the beginning of the tuple
type Unshift<A, T extends Array<any>> = ((a: A, ...b: T) => any) extends ((...result: infer Result) => any) ? Result : never;
```

### Question Pandemia 1 (level middle)
Make type level function which will check if two patients can meet. `CanMeet` should return or true or false depends if patients can or can't meet

```ts
type CanMeet<A extends Patient, B extends Patient> = "Here your implementation 🔥" 

type JohnWithTom = CanMeet<John, Tom> // false as one is sick and anothe is not
type JohnWithKate = CanMeet<John, Kate> // true as both are sick
type DorotyWithGregory = CanMeet<Doroty, Gregory> // false as people in quarantaine cannot meet anybody
type MikeWithTom = CanMeet<Mike, Tom> // false as people in quarantaine cannot meet anybody
type PaulWithTom = CanMeet<Paul, Tom> // true yes both are healty
```
Start your coding in [the Playground](https://www.typescriptlang.org/play/?ssl=26&ssc=66&pln=20&pc=1#code/C4TwDgpgBAyglgYwNZQLxQN4GdgENgQBcUA5FokiQL4BQokUAEhLgDbAAWIamO+RpDi3ZdqdcNACKAV1wAnXADtgcRdHTY8BYiQCOshctyqIY8QwAK+OBGU8MNKFEW4AtgJxzVAcxpUoAGRQABTwyFAAPkzCnNxRMvJKKmoAlADcNObQAFIA9hyK9i7uOnkF1IGwFFlQACq5rkVuAiT1rhVBzGyxNQDS-E0lpP0EHVXINQCycEjqmMUt07NjCYbJEDVW0qyDLVusY10iIDUAIrlyuaC7OueXoCsGSSY1AOJyEN4X3BoLOu+fb5jMJITY2OQIOYYP6kCzgyGPRLKF40AD0qKgMggODguUKVkUABMIK44LgoABGKAAWigrlwsyg9GgrAgADcIDsAGbSRQIFR4qAAdw4iA4wrgrB2CCE4TgXKZQtyUDA1lswCwUAQSjpEAgwEyzKgAGElJM9cAADwAQSgEAAHgQiZqrCp1QAaKAAITtjtshJdauUAD4eAAiZgfKAgXLSORQOCuMCs9xGAWFQC8G4BSXbDUENEigZUUAHU4Jw2jxTYpzfrLUXPW1Q+ioFy2FhoLhNXjoHBNeRwkpCVAlFchAnNYorjUi6XOCM5lWa1b61B502McA5NIO5qAEaj4dR-ugo13K4gWccAFfOQ-E1mi2Ws+gT3X77rlttncqiC5ZM9wp9CRPATC1JRJ2AXV9WHRQQH3QkTiNJYIEvCt0EXR9kIbBoP1bVh22HTVID-VkE0Ap4jFA7VFAgqDIKUODcgQzZcG2VCGkrB9a32bDXA-Tdt2jbEoH3ThD2gIRuhOGggA)

### Question Pandemia 2 (level hard)
Make type level function which will get all sick patients from the collection of patients. `GetSick` should filter the collection for only sick patients.

```ts
type GetSick<Patients extends Patient[]> = "Here your implementation 🔥" 

type Check1 = GetSick<[
  John,
  Tom,
  Kate,
]> // Check1 should be [John, Kate]
type Check2 = GetSick<[
  Gregory,
  Pierce,
  Kate,
  Paul
]> // Check2 should be [Kate, Gregory]
type Check3 = GetSick<[
  Tom,
  Paul
]> // Check3 should be []
```
Start your coding in [the Playground](https://www.typescriptlang.org/play/?ssl=20&ssc=1&pln=36&pc=26#code/C4TwDgpgBAyglgYwNZQLxQN4GdgENgQBcUA5FokiQL4BQokUAEhLgDbAAWIamO+RpDi3ZdqdcNACKAV1wAnXADtgcRdHTY8BYiQCOshctyqIY8QwAK+OBGU8MNKFEW4AtgJxzVAcxpUoAGRQABTwyFAAPkzCnNxRMvJKKmoAlADcNObQAFIA9hyK9i7uOnkF1IGwFFlQACq5rkVuAiT1rhVBzGyxNQDS-E0lpP0EHVXINQCycEjqmMUt07NjCYbJEDVW0qyDLVusY10iIDUAIrlyuaC7OueXoCsGSSY1AOJyEN4X3BoLOu+fb5jMJITY2OQIOYYP6kCzgyGPRLKF40AD0qKgMggODguUKVkUABMIK44LgoAAmKAAWigk1wsyg9GgrAgADcIDsAGbSRQIFR4qAAdw4iA4wrgrB23ggwCgbB25HCYGstmAWCgXMujU40AQuSlEH5uMKuS5UBVKjVWAAdJlmVBXrKQQAeKxW5QaiAADwIRI17psygA2gBdAB8PAARMwPlAQLlpHIoHBXGBWe4jALCoBeDcApLtRqD2iRQADCQmQAEYeE7gK7g44oGVFAAaRttNtOEYQNsRqDossVpDVrAcROsQlQABG0GDzZbUG7oZq5aNSCp6Fr9cbAK+chAnagcIgEJ7je7h-2ND7A9XyCpo-Hk5nUGDF8dHz3IGXDrvSAAzDWzoUC6DZOB2jZXjeGJ-oBj7bM+s6hkAA)

> **Improvement** - Can you make state of the patient as an argument? And make function which will get patients for given condition? Example usage would be `Get<Patients, Healthy>`

### Question Pandemia 3 (level hard)
Help people to be accommodated to the right hospital. Unfortunately they need to be put as a whole into one hospital. Create a function which will tell if hospital has enough free beds for patients. `CanAccomodate` should return true if there is enough place, or false if not.
```ts
type CanAccomodate<Beds extends '🛌'[], Patients extends Patient[]> = "Here your implementation 🔥" 

type Result1 = CanAccomodate<['🛌', '🛌'], [John, Tom]> // true, we have the same amount of beds
type Result2 = CanAccomodate<['🛌', '🛌'], [John]>  // true, we have more beds
type Result3 = CanAccomodate<[], [John]>  // false, we have no beds
type Result4 = CanAccomodate<['🛌', '🛌'], [John, Tom, Doroty]>  // false, we have less beds than patients
```
Start your coding in [the Playground](https://www.typescriptlang.org/play/index.html?ssl=14&ssc=34&pln=14&pc=44#code/C4TwDgpgBAyglgYwNZQLxQN4GdgENgQBcUA5FokiQL4BQokUAEhLgDbAAWIamO+RpDi3ZdqdcNACKAV1wAnXADtgcRdHTY8BYiQCOshctyqIY8QwAK+OBGU8MNKFEW4AtgJxzVAcxpUoAGRQABTwyFAAPkzCnNxRMvJKKmoAlADcNObQAFIA9hyK9i7uOnkF1IGwFFlQACq5rkVuAiT1rhVBzGyxNQDS-E0lpP0EHVXINQCycEjqmMUt07NjCYbJEDVW0qyDLVusY10iIDUAIrlyuaC7OueXoCsGSSY1AOJyEN4X3BoLOu+fb5jMJITY2OQIOYYZzNHQWcGQkhQfxBVbPNSZAD0mKgMggODguUKAGYoABaKAAYSE4TgADMoBxclgwHA8DsOLgsFBbLlpN4OFAAEYQAAm3OAuSgeFm0qEUDA1lswCwNUpSgAgggEA1cqL+AAeABCYu5EAAHgRFOLSIBeDcAM3skADaAF0ADRQKwqZVmy22G1emzKV0APh4ACJmB8oCA+XIoHBXGBWBB3EYVESoLbAKS74agmRqACV8dtgABGHjqxRanWuPWGp0kB0kD1Nx3uqBOsqKD1tF1h7HSuTSCAegDu0E5ADdoJxoFhmlA3Hy7LkGSLxUWS+wAEyVzXa3X6ggGxvN1vNjtd-KKftOQfAYejqATxm4GdQOvRjeq+jQYtYKWpLoFWNZHg2V7dneUCDnSbBYM+r7TtAihSj+W6AewAAs+7VoedbHhAp5ti2drth614FL2DQencVwgNBsHwYhk7vtAKZYNyP5ykoCpKsoWBAA)

### Question Pandemia 4 (level hard)
Segregate all patients in order to block contact between them in order to slow the spreading of the virus. The third task is about full segregation of patients. Pay attention that patients in quarantine are additionally separated from each other by [] 🔒 which represent the fact they cannot meet anybody.
```ts
type Segragate<Patients extends Patient[]> =  "Here your implementation 🔥" 

// Check the result:
type AfterSegregation = Segragate<[
  John,
  Tom,
  Kate,
  Mike,
  Paul,
  Doroty,
]>
/**
 AferSegregation should be
 {
    sick: [Kate, John];
    quarantine: [[Doroty], [Mike]];
    healty: [Paul, Tom];
 }
 */
```
Start your coding in [the Playground](https://www.typescriptlang.org/play/?ssl=45&ssc=4&pln=27&pc=1#code/C4TwDgpgBAyglgYwNZQLxQN4GdgENgQBcUA5FokiQL4BQokUAEhLgDbAAWIamO+RpDi3ZdqdcNACKAV1wAnXADtgcRdHTY8BYiQCOshctyqIY8QwAK+OBGU8MNKFEW4AtgJxzVAcxpUoAGRQABTwyFAAPkzCnNxRMvJKKmoAlADcNObQAFIA9hyK9i7uOnkF1IGwFFlQACq5rkVuAiT1rhVBzGyxNQDS-E0lpP0EHVXINQCycEjqmMUt07NjCYbJEDVW0qyDLVusY10iIDUAIrlyuaC7OueXoCsGSSY1AOJyEN4X3BoLOu+fb5jMJITY2OQIOYYP6kCzgyGPRLKF40AD0qKgMggODguUKVkUABMIK44LgoABmKAAWlgnw+3gGbB2YGstmAWCgqigF2JcigwFyUAARqxcuEEHi8AhgCKIMAAO4QWwCoSNbm8iD8wVQLBihWq6BYMAfXCEnw8gBmhqgADc4HJpFhMuioAAhCD6gUSKAKjiIDg2j5Ybay3LWmD03CMgi6ji5baEuVQdyuYVaq01SPeBn8JMaRy6ijEEEAbQAugAaQv6JHrYil1bPNTliuVqCu1ncfAEZF4mlQM3mlR4thQCtQS0XQtCbogYhHWIVvyZejQbMKGMQAA8VhU7M5EAAHr3CZy9zZlBWAHw8KAAImYHygIAT-LgrjArBJ7Os-cAvBuAKS795QC6GIAMJCOEnDQMGoaEDUACCloEHIG6fH+hToBu0b8NupaFmUijVk4bQkVAIwQORSxUYW+zkXcVwgNW5bXmiABU7GOMhWroTGuKFFg8aJnKjgOE4TjkMgDaUe2RHlhkElQLWawmA2paMaAVbjjR5YKYWTizuw87jvRdQNPpUC0FA7GokAA)

Post your answers in comments (preferred links to the playground). Have fun! Answer will be published soon! Remember these question are not about value level, so we need to use only and only types.

**This series will continue**. If you want to know about new exciting questions from advanced TypeScript please follow me on [dev.to](https://dev.to/macsikora) and [twitter](https://twitter.com/macsikora).

**Be healthy and take care!**