logoalt Hacker News

tshaddoxyesterday at 11:38 PM1 replyview on HN

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).


Replies

culitoday at 12:24 AM

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