DEV Community

Discussion on: Type | Treat Challenge 2

Collapse
 
dungdung profile image
Houston • Edited

If we aren't allowed to change type Ghosts and function investigateReport which accepts an array of Ghosts, can we simply return false for both function areGods and function areEctoplasmic?

My implementation of function areDemons using user-defined type guard:

type Demons = Extract<Ghosts, {sendBackToHell(): void}>
function areDemons(ghosts:Ghosts[]): ghosts is Demons[] {
  return ghosts.every(ghost => (ghost as Demons).sendBackToHell !== undefined)
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
ricklove profile image
Rick Love

Not if you want to live... and save NYC.