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.

TypeScript Enums vs. Flow Enums

7 pointsby GeZeover 3 years ago

2 comments

brundolfover 3 years ago
Many of these problems (some of which I didn&#x27;t even know about because I avoid using enums) go away when you use union types instead. The implicit casting is one example, the display-name retrieval is another (assuming you use string literals for variants)<p>Some of them can worked around by structuring your code in certain ways (for example you could make sure every variant is covered by indexing into an object with `key in MyEnum`, or by returning from each switch case and requiring a particular return type)<p>But even setting those aside, I got burned so badly the last time I used Flow that I&#x27;m not sure anything could convince me to return to it
NoahTheDukeover 3 years ago
This is a significant improvement on Typescript’s enums but I wonder what the underlying code it compiles to is.<p>Because we’re planning on moving our codebase to ESM support (the community is moving over quickly), we’ve run into a number of issues with TS enums, the primary one being that TS enums compile to undefined vars that are populated with an IIFE, which disallows using them in certain import&#x2F;export situations.
评论 #28517655 未加载
评论 #28518206 未加载