$ ./guess_the_languageProblem Solving — fix the bug
Problem Solving — fix the bug · hard · #200

Which fix removes the bug?

type Shape = { kind: 'circle'; r: number } | { kind: 'square'; s: number };
function area(x: Shape): number {
  if (x.kind === 'circle') return Math.PI * x.r ** 2;
}
  1. Handle the square branch
  2. Widen kind to a string
  3. Mark r as optional
  4. Return any from area
Squares fall through and return undefined; an exhaustive switch with a never check catches this.
Play Problem Solving ▸