I wouldn't call this a macro system. It's a preprocessor for another language that is transpiled to typescript.<p>A ts macro system would be integrated with ts language service and tsc. As a compile time language, the main advantage of tsc is, well, developer experience, which, unless I'm missing something, gets thrown out the door here.<p>Also, the demo includes very weak use cases, much of it could be done directly with js functionality without the additional macros. It's not clear to me how much effort and computation time does this saves.
> We can't change the parser<p>Aw :-(<p>That's the bit that I'd really like to have TS macros for!<p>My own approach has been to loosely parse input as something like D-expressions, transform that, and pipe the result into the TS compiler. Crude but effective. The trickiest part is extending the tsserver support, but even this is doable.
> Since WebAssembly can't directly interact with the dom (yet)<p>Going off at a tangent, is there a reason WebAssembly can't interact with the DOM? I would have thought that being able to do so would be kinda useful, particularly in a web page.
The case he describes seems like a really bad example. All of that code could be rewritten to use properly composed functions - a library, for sure, but the complaint isn’t valid. That’s like writing procedural PHP with the database connection defined inline everywhere you use it. There is no need to do so if you can import modules!<p>Are there any actual cases where a macro system in TypeScript would be useful?
Oh dude! is not the full crab but at least a big claw of it lol, congrats on the work is awesome, hope it gets through some more stable versions so we can build more on top of this
This may be a useful tool, but personally every one of the examples seems to illustrate what I would consider to be an anti-pattern in how the code is written.
As an aside I think the non-null assertion operator should be transpiled via Babel from<p>`userId!`<p>to<p>`if (!userId) throw new Error('Non-Null Assertion Failed "userId!"')`
> knows that a huge chunk of why it's so excellent is the macro system<p>Macros are bad and you're bad if you use them. This has been essentially a <i>de facto</i> mantra since the early 90s (when I was a wee lad) and it's quite common to see people writing very long articles on why you shouldn't use them[1]. They are magic, so should be avoided.<p>There's a reason languages like C# or Go don't have them, and you should take heed of the very smart people that designed these languages. Even in Rust, they are controversial[2] (the most obvious reason is that your IDE is worthless when macros are involved). Metaprogramming (i.e. macros) adds extreme complexity (in some cases, pre-runtime-Turing-completeness) with many footguns, including the classic, and very "fun," double evaluation.<p><i>Here be dragons.</i><p>[1] <a href="https://www.linkedin.com/pulse/why-considered-bad-practice-use-macros-c-herbert-elwood-gilliland-iii" rel="nofollow">https://www.linkedin.com/pulse/why-considered-bad-practice-u...</a><p>[2] <a href="https://www.reddit.com/r/rust/comments/taxfe3/what_are_the_pros_and_cons_of_using_macros_in_rust/" rel="nofollow">https://www.reddit.com/r/rust/comments/taxfe3/what_are_the_p...</a>