TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Macro-ts: TypeScript compiler with typesafe syntactic macros (2022)

96 pointsby aabbcc1241almost 2 years ago

9 comments

amitportalmost 2 years ago
I wouldn&#x27;t call this a macro system. It&#x27;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&#x27;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&#x27;s not clear to me how much effort and computation time does this saves.
评论 #36121841 未加载
评论 #36128443 未加载
tonygalmost 2 years ago
&gt; We can&#x27;t change the parser<p>Aw :-(<p>That&#x27;s the bit that I&#x27;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.
评论 #36124528 未加载
评论 #36121590 未加载
评论 #36123260 未加载
cabalamatalmost 2 years ago
&gt; Since WebAssembly can&#x27;t directly interact with the dom (yet)<p>Going off at a tangent, is there a reason WebAssembly can&#x27;t interact with the DOM? I would have thought that being able to do so would be kinda useful, particularly in a web page.
评论 #36121857 未加载
评论 #36121966 未加载
9devalmost 2 years ago
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?
评论 #36122695 未加载
评论 #36122176 未加载
评论 #36128462 未加载
fmirasalmost 2 years ago
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
tengbretsonalmost 2 years ago
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.
mfbx9da4almost 2 years ago
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(&#x27;Non-Null Assertion Failed &quot;userId!&quot;&#x27;)`
评论 #36122767 未加载
mfbx9da4almost 2 years ago
Just use zod?
dvtalmost 2 years ago
&gt; knows that a huge chunk of why it&#x27;s so excellent is the macro system<p>Macros are bad and you&#x27;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&#x27;s quite common to see people writing very long articles on why you shouldn&#x27;t use them[1]. They are magic, so should be avoided.<p>There&#x27;s a reason languages like C# or Go don&#x27;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 &quot;fun,&quot; double evaluation.<p><i>Here be dragons.</i><p>[1] <a href="https:&#x2F;&#x2F;www.linkedin.com&#x2F;pulse&#x2F;why-considered-bad-practice-use-macros-c-herbert-elwood-gilliland-iii" rel="nofollow">https:&#x2F;&#x2F;www.linkedin.com&#x2F;pulse&#x2F;why-considered-bad-practice-u...</a><p>[2] <a href="https:&#x2F;&#x2F;www.reddit.com&#x2F;r&#x2F;rust&#x2F;comments&#x2F;taxfe3&#x2F;what_are_the_pros_and_cons_of_using_macros_in_rust&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.reddit.com&#x2F;r&#x2F;rust&#x2F;comments&#x2F;taxfe3&#x2F;what_are_the_p...</a>
评论 #36121850 未加载
评论 #36121554 未加载
评论 #36123290 未加载
评论 #36125833 未加载
评论 #36121436 未加载