Cover image for TypeScript Exercises Bonus🦠 - Type of Pandemia

TypeScript Exercises Bonus🦠 - Type of Pandemia

macsikora profile image Maciej Sikora Updated on ・4 min read

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. 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

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:

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:

// 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

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

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.

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

type Check1 = GetSick<[
]> // Check1 should be [John, Kate]
type Check2 = GetSick<[
]> // Check2 should be [Kate, Gregory]
type Check3 = GetSick<[
]> // Check3 should be []

Start your coding in the Playground

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.

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

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.

type Segragate<Patients extends Patient[]> =  "Here your implementation 🔥" 

// Check the result:
type AfterSegregation = Segragate<[
 AferSegregation should be
    sick: [Kate, John];
    quarantine: [[Doroty], [Mike]];
    healty: [Paul, Tom];

Start your coding in the Playground

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 and twitter.

Be healthy and take care!


Editor guide
nombrekeff profile image
Manolo Edge

Thanks for this man! Will keep some minds entertained for a little while.

I will give it a go this afternoon!
Keep safe!