u/AtmosphereDefiantin r/typescript • Recommends
#typescriptenums#enums#typescript
TypeScript Enums are TERRIBLE. Here's Why.
22K subscribers • 12 videos on Hallway
- Recommended by 1 person: 1 in engineering communities.
- An opinion. It argues one side of a question engineers disagree on.
Intermediate. Assumes working knowledge.
75K views3 years ago1 recommendation
Programming languagesTypeScriptLanguage design and type systems
An exploration of the runtime and design issues with TypeScript enums and better alternatives like plain objects or string unions.
TypeScript enums have worn their welcome. In the early days before string unions became so powerful, there was no other option. Today, though, they are more trouble than they're worth. This talk explores some of the problems with enums as well as some solutions to those problems. No solution is perfect, but perhaps no perfect solution uses TypeScript's enums, either. :P how enums work in general the value of enums by default the first failure of enums attempting to fix this problem with required indices what do enums look like at runtime? another downside: function arguments working around the problem with function arguments there are footguns (even with workarounds) a better approach than enums: POJOs audience question: why not just create a string union? audience question: runtime values for a bad function argument? audience comment: it's still just JavaScript audience question: are there any keys on the enum? audience comment: TypeScript runtime artifacts === bad audience question: how do you deal with variations? the satisfies operator could help how to solve a new problem introduced by using objects Aaron Harper (github.com/goodoldneon) is a software engineer who enjoys gardening, biking, and spending too much time and money on an overkill homelab. Recorded on November 1st 2022 at Cahoots (cahoots.com) in Ann Arbor, Michigan.| Discord: discord.gg/tmhGfR4Dye Twitter: twitter.com/MiTypeScript Mastodon: fosstodon.org/@MichiganTypeScript Instagram: instagram.com/michigantypescript TikTok: tiktok.com/@michigantypescriptWhy it's here
1 person recommends thisWhat people say. Recommended as a clear explanation of the pitfalls of TypeScript enums and why to avoid them.
Moments people linked to
u/ConsiderationMany246in r/typescript • Mentions
On “enum vs "as const" vs Object.freeze() what is the difference between those 3?”
u/it-birdsin r/typescript • Mentions
On “What's the common way to define large number of constants?”
u/Pleasant-Fish7370in r/typescript • Mentions
On “What's up with all the enum hate lately?”
u/it-birdsin r/typescript • Mentions
On “8 Tricks to write clean code in TypeScript - CodingHelp.io”
u/it-birdsin r/typescript • Mentions
On “How to get available obj keys when using Record?”
u/got_no_time_for_thatin r/typescript • Mentions
On “How to use enum as key when define interface?”