Skip to content
0:00 / 0:00

#typescriptenums#enums#typescript

TypeScript Enums are TERRIBLE. Here's Why.

Michigan TypeScript

22K subscribers • 12 videos on Hallway

YouTube
  • 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/@michigantypescript

Why it's here

1 person recommends this

What people say. Recommended as a clear explanation of the pitfalls of TypeScript enums and why to avoid them.

Moments people linked to

u/AtmosphereDefiantin r/typescript • Recommends

TypeScript Enums are Terrible. Here's Why.

u/ConsiderationMany246in r/typescript • Mentions

On “enum vs "as const" vs Object.freeze() what is the difference between those 3?”

enums are weird in TS, if you want to know Enums are terrible presentation const Direction = { Up =: 1, Down:2, Left:3, Right:4, } as const type Direction = keyof typeof Direction is probably the best approach
15View on Reddit

u/it-birdsin r/typescript • Mentions

On “What's the common way to define large number of constants?”

Yes, but I would recommend to avoid the actual ```enum``` keyword. Just check out this video and then decide if you want to use it or not: link to this video

u/Pleasant-Fish7370in r/typescript • Mentions

On “What's up with all the enum hate lately?”

I'm guessing it started with this talk. I'm surprised too since enums are not something new, they've been around for a while. I think everyone just jumped on the lets-hate-on-enums bandwagon.

u/it-birdsin r/typescript • Mentions

On “8 Tricks to write clean code in TypeScript - CodingHelp.io”

Nice article. Here some thoughts from my side. ```Interface``` vs ```type``` I generally tend to only use ```type``` (here a good explanation why as video link to this video Because ```interface```s in TS come with the nasty feature interface merging. That can be helpful with globals, but generally you don't want it. ```enum```s It has gotten better with TS 5.0, but still not perfect. Here a nice source that outlines the problem and shows a.good alternative: link to this video

u/it-birdsin r/typescript • Mentions

On “How to get available obj keys when using Record?”

I would recommend a const object, as typescript enums are strange at runtime: link to this video

u/got_no_time_for_thatin r/typescript • Mentions

On “How to use enum as key when define interface?”

Think your link is broken: TypeScript is TERRIBLE Title is also a huge exaggeration Here's an alternative perspective, from the same meetup: ENUMS are MISUNDERSTOOD