← Back to archive

Stdle #23 · narrowing · 2026-06-29

What does this TypeScript snippet output?

A past puzzle — fully playable. 4 attempts, hints on wrong guesses.

type Shape =
| { kind: "circle"; r: number }
| { kind: "square"; s: number }
function area(sh: Shape): number {
switch (sh.kind) {
case "circle": return 3 * sh.r * sh.r
case "square": return sh.s * sh.s
}
}
console.log(area({ kind: "square", s: 4 }))
Narrowing
4 attempts left

Answer & explanation