TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

TypeScript is surprisingly ok for compilers

300 点作者 Fudgel将近 2 年前

20 条评论

grumblingdev将近 2 年前
TypeScript is an incredible language in general.<p>The fact that Functions are Objects that can have properties&#x2F;methods is supremely undervalued.<p>Are there other languages that do this so nicely? It&#x27;s the perfect blend of OO and functional.<p>Programming is mostly about gradually figuring out the right design I find. JS&#x2F;TS let&#x27;s me evolve things naturally without big rewrites.<p><pre><code> function foo() {} function bar() {} function baz() {} const commands = [foo, bar, baz] &#x2F;&#x2F; Run commands commands.forEach(x =&gt; x()) foo.help = &#x27;This does something&#x27; &#x2F;&#x2F; Describe commands commands.forEach(x =&gt; console.log(x.help)) &#x2F;&#x2F; Add some state via closure. const config = {} function foo() { config.blah } &#x2F;&#x2F; Add some state using partials. function _foo(config) { config.blah } const foo = foo.bind(null, config) </code></pre> I can flexibly do what I need, without ever having to define a class like `Command`, which I probably don&#x27;t even know what it should be yet.<p>This premature naming of things in OO creates so many dramas.<p>It avoids things like `VideoCompressor#compress()`, `VideoSplitter#split()`. You don&#x27;t have to think: what state belongs in which class...you just call the function and pass it what it needs.
评论 #37178167 未加载
评论 #37178341 未加载
评论 #37175527 未加载
评论 #37176895 未加载
评论 #37178203 未加载
评论 #37181918 未加载
评论 #37177932 未加载
评论 #37179544 未加载
评论 #37177331 未加载
评论 #37179666 未加载
评论 #37179830 未加载
评论 #37178185 未加载
评论 #37177677 未加载
评论 #37182008 未加载
评论 #37178852 未加载
评论 #37177876 未加载
评论 #37219508 未加载
评论 #37178726 未加载
lmm将近 2 年前
Is that really suprising? Typescript is yet another language that has, kicking and screaming, picked up most of the ML featureset. I&#x27;d expect it to be, well, fine; the lack of real pattern matching is a pain, so it&#x27;s going to be inferior to OCaml, but fine, no different from using C# or Swift or Dart or Kotlin or something of that ilk.
评论 #37172349 未加载
评论 #37177534 未加载
评论 #37172412 未加载
评论 #37172318 未加载
评论 #37172850 未加载
评论 #37172383 未加载
评论 #37173588 未加载
评论 #37172199 未加载
评论 #37177152 未加载
评论 #37173244 未加载
评论 #37172944 未加载
评论 #37172097 未加载
评论 #37172955 未加载
gr__or将近 2 年前
Very much apropos:<p>Going between Rust and TS it is painfully obvious how much sth like tagged enums are missing, which can also be seen in this post.<p>I know of this [1] proposal for ADT enums which looks like it has stalled. Anyone know of other efforts?<p>[1] <a href="https:&#x2F;&#x2F;github.com&#x2F;Jack-Works&#x2F;proposal-enum&#x2F;discussions&#x2F;19">https:&#x2F;&#x2F;github.com&#x2F;Jack-Works&#x2F;proposal-enum&#x2F;discussions&#x2F;19</a>
评论 #37176956 未加载
rtpg将近 2 年前
TS&#x27;s type system is fun but a part of me always wonders how much faster TS&#x27;s compiler would be if it was written in a compiled language (assuming &quot;good implementation&quot;, which is a big assumption!)
评论 #37172559 未加载
评论 #37172732 未加载
评论 #37175799 未加载
评论 #37173313 未加载
评论 #37175641 未加载
评论 #37172433 未加载
评论 #37173038 未加载
评论 #37172278 未加载
评论 #37176568 未加载
评论 #37175460 未加载
dna_polymerase将近 2 年前
It sure is. For anyone looking into Compilers and just starting out, I recommend this book: <a href="https:&#x2F;&#x2F;keleshev.com&#x2F;compiling-to-assembly-from-scratch&#x2F;" rel="nofollow noreferrer">https:&#x2F;&#x2F;keleshev.com&#x2F;compiling-to-assembly-from-scratch&#x2F;</a><p>The author uses a TypeScript subset to write a compiler to 32bit ARM assembly and explains that it almost looks like Pseudocode, so it is very accessible. A sentiment I can get behind, despite avoiding it in any case possible.
评论 #37174635 未加载
评论 #37173146 未加载
评论 #37180658 未加载
评论 #37179903 未加载
domlebo70将近 2 年前
To OP: You could avoid the visitor by using an IIFE style switch using the run utility fn:<p><pre><code> export const run = &lt;T&gt;(f: () =&gt; T): T =&gt; f(); </code></pre> Now you can go:<p><pre><code> const inferred_type = run(() =&gt; { switch(blah) { ... } })</code></pre>
评论 #37174075 未加载
评论 #37179870 未加载
andrewcobby将近 2 年前
As some how is writing a compilter in TS, I agree it&#x27;s not too bad. I started with Deno like the author but ended up switching to Bun which, despite some rough edges, I&#x27;m enjoying more than Deno and it&#x27;s _very_ quick! (My main niggle with Bun is the test reporting, but it&#x27;s getting the job done)<p>For standard parser generator frontend, Ohm-js[1] is quite pleasant. I wouldn&#x27;t recommend anyone reviews the offical tsc compiler, it&#x27;s huuuge - instead there&#x27;s mini-typescript[2] (in particular the centi-typescript branch[3]) which does a nice job illustrating how tsc is working.<p>[1] <a href="https:&#x2F;&#x2F;ohmjs.org&#x2F;" rel="nofollow noreferrer">https:&#x2F;&#x2F;ohmjs.org&#x2F;</a> [2] <a href="https:&#x2F;&#x2F;github.com&#x2F;sandersn&#x2F;mini-typescript&#x2F;">https:&#x2F;&#x2F;github.com&#x2F;sandersn&#x2F;mini-typescript&#x2F;</a> [3] <a href="https:&#x2F;&#x2F;github.com&#x2F;sandersn&#x2F;mini-typescript&#x2F;tree&#x2F;centi-typescript">https:&#x2F;&#x2F;github.com&#x2F;sandersn&#x2F;mini-typescript&#x2F;tree&#x2F;centi-types...</a><p>Looking forward to GC an DOM access in WASM.
评论 #37187807 未加载
catsarebetter将近 2 年前
That is surprising, I would&#x27;ve thought that TS would have more overhead because of the interfaces adding extra weight. Makes me wonder what else TS could apply to. Language parsing, maybe?
评论 #37172599 未加载
paxys将近 2 年前
The result shouldn&#x27;t be all that surprising considering the TypeScript compiler is written in...TypeScript. So TypeScript as a ML is already battle tested and is in heavy production use every day.
hardware2win将近 2 年前
Ive been writing compiler in C# and I dont see anything fancy here except union type<p>Ive personally decided to avoid visitor pattern bloat and Im waiting for closed enum feature in order to have compile time exhaustive check
评论 #37174191 未加载
rustybolt将近 2 年前
I really don&#x27;t know what the author means by &quot;language-centric&quot; vs &quot;implementation-centric&quot; and &quot;production-ready&quot;...
评论 #37173701 未加载
andromaton将近 2 年前
Can I humbly add &quot;Anders Hejlsberg&quot; to the list of reasons why none of the commenters seem surprised?
auggierose将近 2 年前
Not that surprising: <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=37039443">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=37039443</a><p>Of course, that one has been downvoted to -4 (as of now).
metalforever将近 2 年前
It’s just not the right tool for the job .
评论 #37185428 未加载
boredumb将近 2 年前
every day we stray further from God
harha_将近 2 年前
I&#x27;m sick of seeing overly complicated TypeScript code. Mainly overly complicated types.
评论 #37176141 未加载
评论 #37174887 未加载
tomp将近 2 年前
As the post nicely demonstrates, TypeScript is definitely <i>not</i> OK for compilers (and not surprising at all!)<p>It doesn&#x27;t even have destructuring pattern matching!<p>At this point, even Java is better [1].<p>[1] <a href="https:&#x2F;&#x2F;github.com&#x2F;tomprimozic&#x2F;caya&#x2F;blob&#x2F;master&#x2F;src&#x2F;caya&#x2F;Interpreter.java#L250">https:&#x2F;&#x2F;github.com&#x2F;tomprimozic&#x2F;caya&#x2F;blob&#x2F;master&#x2F;src&#x2F;caya&#x2F;Int...</a>
评论 #37173069 未加载
评论 #37173464 未加载
评论 #37173332 未加载
frozenport将近 2 年前
Gut reaction is to use LLVM for everything...
评论 #37174959 未加载
评论 #37173924 未加载
newusertoday将近 2 年前
it isn&#x27;t, i have codebase in golang that is much larger than typescript and it is pleasant to work with lsp and compiles smoothly. With typescript i have to turn off lsp and even after that it takes long time too compile. There is a reason why people are writing typescript compiler in rust.
评论 #37172306 未加载
评论 #37172368 未加载
zelphirkalt将近 2 年前
But to what end? For a compiler, there is no need for all the overhead of npm (usually), ts, tsconfig, package.json bundler and bundler configuration, to get something usable, unless one really wants a JS thing at the end to run in the browser. I imagine even some webassembly tool chains may be shorter.
评论 #37172367 未加载
评论 #37172363 未加载