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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

In Defence of TypeScript Enums

19 点作者 romellem大约 1 个月前

5 条评论

TheCycoONE大约 1 个月前
I think the author missed that we are criticising typescript&#x27;s enum implementation not the concept of enums in general.<p>They transcribe to something better defined in javascript. Typescript itself akwardly sits on the fence of almost entirely erasable and some decisions only make sense if that was the goal, like import files having a js rather than the ts extension that would point to the file in source.<p>Enums become javascript objects in a very leaky abstraction full of footguns. There are plenty of articles that cover these and this author seems to have written them off as &#x27;not holding it right&#x27; but a good language feature fits in the hand naturally to stretch the analogy.
miningape大约 1 个月前
This is the problem with typescript enums, they&#x27;re close enough to &quot;normal&quot; enums in other &quot;real&quot; languages that there aren&#x27;t any hard edges at first glance. Luring you in with a false sense of comfort and familiarity.<p>Then abstraction starts leaking everywhere because it&#x27;s trying to bridge between typescript and javascript and you start wishing you had either made an object (pure JS) or a literal union (pure TS).
koshdim大约 1 个月前
Nice article, clearly defines the scope for enums and why it is so in TS. I had the same feeling and use enums a lit less than in C#, because of mentioned limitations, but they&#x27;re still useful for right purpose
tengbretson大约 1 个月前
`if (color === &quot;Red&quot;) {`<p>This counter example against using unions of literals would result in a compiler error ts(2367). Which only furthers the argument for using unions of literals instead.
评论 #43551977 未加载
paulddraper大约 1 个月前
&gt; Enums Are Meant To Be Symbolic<p>And yet you can’t use symbols [1].<p>Author gives a valiant effort, but is defending the indefensible.<p>[1] <a href="https:&#x2F;&#x2F;github.com&#x2F;microsoft&#x2F;TypeScript&#x2F;issues&#x2F;18408" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;microsoft&#x2F;TypeScript&#x2F;issues&#x2F;18408</a>