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.

An ode to TypeScript enums

57 pointsby disintegrator3 months ago

12 comments

spankalee3 months ago
Const objects really are better than enums, in every way except declaration brevity.<p>They&#x27;re erasable syntax, so they work in environments that just strip types. Their emit is just what you write without the types. They can be composed in a type-safe way with standard JS operations.<p>You can still write JS docs for values, deprecated the, mark them as internal, etc.<p><pre><code> type ValueOf&lt;T&gt; = T[keyof T]; const Foo = { &#x2F;** * A one digit * @deprecated *&#x2F; one: &#x27;1&#x27;, two: &#x27;2&#x27;, three: &#x27;3&#x27; } as const; type Foo = ValueOf&lt;typeof Foo&gt;; const Bar = { blue: &#x27;blue&#x27;, } as const; type Bar = ValueOf&lt;typeof Bar&gt;; &#x2F;&#x2F; You can union enum objects: const FooOrBar = {...Foo, ...Bar}; &#x2F;&#x2F; And get union of their values: type FooOrBar = ValueOf&lt;typeof FooOrBar&gt;; const doSomething = (foo: Foo) =&gt; {} &#x2F;&#x2F; You can reference values just like enums: doSomething(Foo.two); &#x2F;&#x2F; You can also type-safely reference enum values by their &#x2F;&#x2F; key name: doSomething(Foo[&#x27;two&#x27;]); </code></pre> Given the TypeScript team&#x27;s stance on new non-erasable syntax, I have to think this is how they would have gone if they had `as const` from the beginning. Ron Buckton of the TS team is championing an enum proposal for JS: <a href="https:&#x2F;&#x2F;github.com&#x2F;rbuckton&#x2F;proposal-enum">https:&#x2F;&#x2F;github.com&#x2F;rbuckton&#x2F;proposal-enum</a> Hopefully that goes somewhere and improves the declaration side of thigns too.
评论 #43234314 未加载
评论 #43234214 未加载
评论 #43234177 未加载
评论 #43279724 未加载
评论 #43235180 未加载
zdragnar3 months ago
Seeing posts like these, I often feel alone preferring enums to string unions.<p>There are certain situations where refactoring a string in a union will not work but refactoring an enum will. I don&#x27;t want to type strings when, semantically, what I want is a discrete type. I don&#x27;t even care that they become strings in JS, because I&#x27;m using them for the semantic and type benefits, not the benefits that come with the string prototype.
评论 #43234053 未加载
评论 #43238295 未加载
评论 #43234062 未加载
评论 #43234101 未加载
bubblyworld3 months ago
One thing I find useful about enums is that they can be used as both types and values, which is ergonomic for decorator-based libraries (like class-validator, nestjs, mikro-orm, etc). The best approach I&#x27;ve found in union land is using <i>const</i> assertions and <i>typeof</i>, which I don&#x27;t love.<p>Agree with the author that in almost every other way unions are better though... they play much more nicely with the rest of the type system. I find it endlessly annoying that I have to refer to enum members directly instead of just using literals like you can with union types.
评论 #43233655 未加载
motorest3 months ago
Can anyone explain why enums are somehow bad but literal unions are supposed to be good?<p>I&#x27;ll be blunt: at the surface level, it looks like literal unions are something that only someone with an irrational axe to grind against enums would ever suggest as a preferable alternative just to not concede that enums are fine.<p>If the problem lies in the low-level implementation details of enums, I cannot see any reason why they shouldn&#x27;t be implemented the same way as literal unions.<p>So can anyone offer any explanation on why enums should be considered bad but literal unions should be good?
评论 #43235968 未加载
wruza3 months ago
Parameter properties also gone? I only recently found out about these, so useful for data-ish classes.
评论 #43233879 未加载
DanielHB3 months ago
General programming languages theory question, is one supposed to iterate over enum entries or is that considered an antipattern? I have found myself needing to do that a few times and it always felt a bit dirty.
评论 #43234806 未加载
评论 #43234918 未加载
forty3 months ago
This is my preferred home made way of doing &quot;Enum&quot; in TS theses days <a href="https:&#x2F;&#x2F;gist.github.com&#x2F;forty&#x2F;ac392b0413c711eb2d8c628b3e769896" rel="nofollow">https:&#x2F;&#x2F;gist.github.com&#x2F;forty&#x2F;ac392b0413c711eb2d8c628b3e7698...</a> - it includes syntax to migrate from TS enum.<p>The member documentation point is a good one, I&#x27;ll look what can be done with my solution.
评论 #43235094 未加载
MBCook3 months ago
What do people find works better as a string enum replacement?<p><pre><code> const Thing { one: “one”, two: “two”, three: “three” } as const </code></pre> Or just<p><pre><code> type Thing = “one” | “two” | “three” </code></pre> I’ve been thinking of getting rid of the simple string enums I have but it’s not clear to me why one is preferred over the other by people.
评论 #43234691 未加载
评论 #43244050 未加载
homebrewer3 months ago
const enums are almost never mentioned by these articles for some reason. They give you the best of both worlds: they&#x27;re fully erasable, and have good LSP support (do no need to search for strings and bump into false matches — or even worse, for numbers).
评论 #43233916 未加载
评论 #43235073 未加载
o11c3 months ago
The problem with &quot;just use literal strings&#x2F;numbers&quot; is that that&#x27;s exactly the <i>opposite</i> of type safe. With them it is impossible to specify an argument of type `myenum | number | string`, despite that being commonly desired in some form.<p>When targeting javascript, it seems to me that the obvious approach is to use <i>symbols</i> for enums. But symbols have a lot of WET.<p>(of course, typescript&#x27;s safety is unfixably broken in numerous other ways, so why bother?)
DidYaWipe3 months ago
&quot;Probably my favorite argument in steelmanning enums&quot;<p>Whatever that&#x27;s supposed to mean.
评论 #43235483 未加载
lelandfe3 months ago
&gt; <i>TypeScript 5.8 is out bringing with it the --erasableSyntaxOnly flag</i><p>TypeScript sure loves the &quot;our only documentation lives in the changelog&quot; approach to stuff, huh?<p>- The on-site Algolia search returns 0 results for &quot;erasableSyntaxOnly&quot;<p>- The blocked-from-search release notes[0] looks like actual documentation - but urges to check out the PR[1] &quot;for more information,&quot; despite the PR description being essentially blank.<p>- The CLI options page[2] describes it thus: &quot;Do not allow runtime constructs that are not part of ECMAScript,&quot; with no links to learn more about what that means.<p>Edit: actually, I take it back! Clicking the flag on the CLI page takes one to an intimidating junk drawer page... but my issues with discoverability stand: <a href="https:&#x2F;&#x2F;www.typescriptlang.org&#x2F;tsconfig&#x2F;#erasableSyntaxOnly" rel="nofollow">https:&#x2F;&#x2F;www.typescriptlang.org&#x2F;tsconfig&#x2F;#erasableSyntaxOnly</a><p>[0] <a href="https:&#x2F;&#x2F;www.typescriptlang.org&#x2F;docs&#x2F;handbook&#x2F;release-notes&#x2F;typescript-5-8.html#the---erasablesyntaxonly-option" rel="nofollow">https:&#x2F;&#x2F;www.typescriptlang.org&#x2F;docs&#x2F;handbook&#x2F;release-notes&#x2F;t...</a><p>[1] <a href="https:&#x2F;&#x2F;github.com&#x2F;microsoft&#x2F;TypeScript&#x2F;pull&#x2F;61011">https:&#x2F;&#x2F;github.com&#x2F;microsoft&#x2F;TypeScript&#x2F;pull&#x2F;61011</a><p>[2] No idea why on-site search doesn&#x27;t pick this up: <a href="https:&#x2F;&#x2F;www.typescriptlang.org&#x2F;docs&#x2F;handbook&#x2F;compiler-options.html" rel="nofollow">https:&#x2F;&#x2F;www.typescriptlang.org&#x2F;docs&#x2F;handbook&#x2F;compiler-option...</a>
评论 #43234381 未加载
评论 #43234357 未加载