Skip to content
0:00 / 0:00

#enums#asconst#typescript

Enums considered harmful

Matt Pocock

429K subscribers • 9 videos on Hallway

YouTube
  • Recommended by 5 people: 5 in engineering communities.
  • Presented by Matt Pocock, TypeScript educator
  • An opinion. It argues one side of a question engineers disagree on.

Intermediate. Assumes working knowledge.

256K views3 years ago5 recommendations

Programming languagesTypeScriptLanguage design and type systems

Matt Pocock explains why TypeScript enums have design flaws and why using object constants with as const is a better alternative.

TypeScript enums are not great. In almost all cases, I use an 'as const' with a sprinkling of type magic to express them. Become a TypeScript Wizard with Matt's TypeScript Course: totaltypescript.com/ Follow Matt on Twitter twitter.com/mattpocockuk

Why it's here

5 people recommend this

What people say. Engineers frequently point to this video to explain why TypeScript enums should be avoided in favor of object constants.

u/derNikoDemin r/typescript • Recommends

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

I found this video very insightful, which talks mainly about enums but the author explains why he prefers using as const link to this video

u/Wrexesin r/typescript • Recommends

On “String union type of enum?”

Matt Pocock has a really good 10min link to this video about why TS \`enum\` keyword *shouldn't* be used. It's not really that POJOs have advantages over `enum`s, it's that `enum`s have dumb disadvantages compared to POJOs. Also, I personally like using an array because it allows for snappy runtime checks and easy testing with methods such as `.includes()`, `.some()`, etc.

u/Worldly-Froyo4804in r/typescript • Recommends

On “What's the preferred way to create an object of constants and then use it as a type?”

thanks for the tip on Matt Pocock. This was interesting about enums. Perhaps this is the vid you were referring to: [video

u/Infinitehin r/typescript • Recommends

On “How to get this typing behavior?”

I'd do sth like this ``` const Env = { PROD: "PROD", DEV: "DEV", } as const; type Env = typeof Envkeyof typeof Env const someFunc = (env: Env) => { // do sth } someFunc('lol'); // Error someFunc('DEV'); // works someFunc(Env.PROD); // works ``` link to this video

u/phischer_hin r/typescript • Recommends

On “Safer exhaustive switch statements in TypeScript”

IIRC: Not type safe in many cases, larger bundle size, convoluted, typescript team would remove them if they could (doesnt fit with its philosophy), other concepts solve the same problem in TS better. link to this video

u/Infinitehin r/typescript • Recommends

On “Review Request: Which one is better and why?”

I think this can also be solved like so: const SmartFridgeCommands = { START: 0b0001, // etc } as const; type ObjectValues<T> = Tkeyof T; type SmartFridgeCommand = ObjectValues<typeof SmartFridgeCommands> function commandSmartFridge(comm: SmartFridgeCommand) { // make the fridge do something } // These will work commandSmartFridge(SmartFridgeCommands.START); commandSmartFridge(0b0001); commandSmartFridge(1); // this wont't, compiler error commandSmartFridge('hello'); See link to this video Take the title with a grain of salt :)

u/hro_in r/typescript • Mentions

On “Force enum syntax”

Like this? link to this video

u/besthelloworldin r/typescript • Mentions

On “Force enum syntax”

link to this video

u/Wrexesin r/typescript • Mentions

On “Do I put enums in types.ts?”

Don't use enums. Period. There's a myriad of explanations on why they're bad all over in the internet, and recommendations on why not to use them and what to use instead. Matt Pocock has a great link to this video about it. The takeaway is that a const object is generally just a safer and more idiomatic way to do it.

u/sagabanin r/typescript • Mentions

On “What's the preferred way to create an object of constants and then use it as a type?”

[video