Comment by tshaddox
5 hours 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).
This does act exactly as a discriminated union. The code works exactly as written.