macro-ts: an ergonomic typescript compiler that enables typesafe syntactic macros | Blaine Hansen

submited by
Style Pass
2023-05-30 03:00:11

Anyone who's worked with Rust (opens new window) knows that a huge chunk of why it's so excellent is the macro system. When using a statically typed language, there are simply so many programs that will have mountains of boilerplate if they can't automatically generate code.

Why is this the case? Well, when you've committed to using a typesafe language (which you should), there are many small tasks (such as mapping one type to another, or validating a type, or constructing a type with defaults) where the code simply mirrors the structure of the types, and can easily be generated by a rote algorithm. With this capability, the potential tedium of writing typesafe code can be largely removed, and the language is a joy. But without it, the language can become a chore. Typescript is one such language, and it's insanely incomplete without real macros.

Since WebAssembly can't directly interact with the dom (yet) (opens new window) , it's an unfortunate reality for some of us that we can't use Rust in all our projects. So when we need to write browser code that performs well, typescript is the only available option that's acceptably typesafe (I'm a bit of a typesafety fundamentalist 😅). But typescript doesn't have macros (opens new window) , and it doesn't seem the core team is terribly interested in the idea. So I decided to do it myself.

Leave a Comment