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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Pattern Matching Custom Data Types in TypeScript

42 点作者 dested超过 6 年前

7 条评论

sfvisser超过 6 年前
It&#x27;s a neat trick and I&#x27;ve used similar patterns in some of our projects, but in the end it doesn&#x27;t buy you much over a `switch (x.kind) { ... }`. (Except your matcher can be used as a proper expression which is nice obviously)<p>But you don&#x27;t have proper pattern nesting, fall-through semantics and it&#x27;s not clear (to me yet) how to extend to matching on multiple values. Try implementing an equality function on a sum-type using pattern matching.<p>Having actual proper pattern matching added to TS would be such a great addition. Especially with full support of the control flow analysis.
评论 #19065851 未加载
maxxxxx超过 6 年前
I think the code is way too verbose. One supposed advantage of FP languages is that they are more concise. If I have to build a ton of boilerplate I might just as well go to some OOP pattern with interfaces and so on.
评论 #19065835 未加载
swissmanu超过 6 年前
i did an article about quite a similar solution approach some time ago: <a href="https:&#x2F;&#x2F;pattern-matching-with-typescript.alabor.me&#x2F;" rel="nofollow">https:&#x2F;&#x2F;pattern-matching-with-typescript.alabor.me&#x2F;</a><p>having real pattern matching built in to TS would be awesome. nice write up though!
评论 #19066116 未加载
Tarean超过 6 年前
Since typescript is compiled anyway it feels somewhat silly not to have the compiler do this work. I wonder how hard it would be to make the desugaring extensible, maybe with a rewrite engine like what Hermit does in haskell.
mattigames超过 6 年前
I&#x27;m disappointed in TypeScript not having pattern matching, multiple classes inheritance, and operator overloading (a MUST for game development)
评论 #19067615 未加载
PudgePacket超过 6 年前
Don&#x27;t think the &quot;never&quot; default arm is necessary. If the return type is set and you miss a case TS should complain.
jaunkst超过 6 年前
You could use a typescript decorator to make it less verbose to setup I suppose. I would rather have a more native approach.
评论 #19066142 未加载
评论 #19066137 未加载