Handling errors in JavaScript/TypeScript kinda sucks. throw
ing breaks the control flow, is not "discoverable" (e.g. impossible to really know what will happen in error states), and it is generally more tricky to get well typed error responses.
Taking inspiration from Rust's Result
type, we can implement something similar in TypeScript with a minimal amount of code and no dependencies. It's not as robust as Rust's of course, but it has been very useful for me on a variety of large projects.
Save the following TypeScript in your project and import it to use the various result utils: