DEV Community

Discussion on: TypeScript Exercises Bonus🦠 - Answers Part 1

Collapse
 
regevbr profile image
Regev Brody

Another solution (that already includes the generic solution):

type State = Sick | Healthy | Quarantine

type Head<T extends Array<any>> = T extends [infer H, ...any[]] ? H : never

type GetByState<Patients extends Patient[], S extends State, Acc extends Patient[] = []> = 
Patients['length'] extends 0
  ? Acc
  : Head<Patients> extends S & Patient
      ? GetByState<Shift<Patients>, S, Unshift<Head<Patients>, Acc>>
      : GetByState<Shift<Patients>, S, Acc>

type GetSick<Patients extends Patient[]> = GetByState<Patients, Sick>
Enter fullscreen mode Exit fullscreen mode

playground