Rust-like Enums in TypeScript - 2019-03-08 00:00:00

One of the nicest things about Rust is handling nulls with Option and error states with Result. Because Rust has good pattern matching, handling these cases becomes trivial as the complier forces you to handle all the cases of an enum in a match expression. Because TypeScript lacks pattern matching, we have to lean on the type system in a different way to get similar semantics. For Option, we start with defining the two different states and the union type that represents the Option: