Hacker News new | ask | show | jobs
by tshaddox 100 days ago
The idiomatic way to do this in TypeScript is with discriminated unions. You’re basically just giving the type system an extra property that makes it trivial to infer a type guard (while also making the runtime check in the compiled JavaScript foolproof).
1 comments

This does act exactly as a discriminated union. The code works exactly as written.