TSworkingts-evergreen
Discriminated Unions
Tag your variants.
Give every variant in a union a unique literal "tag" property — and TypeScript will narrow on it for free, with exhaustiveness checking thrown in.
Shape: Circle | Square | Triangle
TSintro Press Run to execute
Switching on the kind tag narrows each branch to one variant, so s.radius, s.side, and s.base/s.height are only accessible where they exist — yielding 78.54, 16, and 6. With every variant handled, TypeScript accepts the function as returning number even without a default.