Explore Library
Code Quiz

Narrowing a Discriminated Union

Spot the narrowing mistake when handling a discriminated union of shape interfaces.

Codetypescript
interface Circle {
  kind: "circle";
  radius: number;
}

interface Square {
  kind: "square";
  side: number;
}

type Shape = Circle | Square;

function area(shape: Shape): number {
  if (shape.kind === "circle") {
    return Math.PI * shape.radius ** 2;
  }
  return shape.side * shape.radius;
}

What is the bug in this code?