6

What are the pros and cons of using an enum vs a type alias as below?

const enum LanguageEnum {
  English,
  Spanish
}

type LanguageAlias = "English" | "Spanish";

let l: LanguageAlias = "English";

let la: LanguageEnum = Language.English

The goal of using the enum is to enumerate the possible values for a type. But doesn't the union of string literals also enumerate the possible types?

I've always found these TS enums to be so weird.

evianpring
  • 3,316
  • 1
  • 25
  • 54
  • 4
    Related: [Typescript has unions, so are enums redundant?](https://stackoverflow.com/q/40275832/215552) – Heretic Monkey Nov 12 '19 at 00:23
  • Does this answer your question? [Typescript has unions, so are enums redundant?](https://stackoverflow.com/questions/40275832/typescript-has-unions-so-are-enums-redundant) – numéro6 Mar 16 '21 at 10:03

0 Answers0