Skip to content

Comparison with native string enums (TypeScript 2.4) #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
benny-medflyt opened this issue Jun 22, 2017 · 3 comments
Closed

Comparison with native string enums (TypeScript 2.4) #26

benny-medflyt opened this issue Jun 22, 2017 · 3 comments

Comments

@benny-medflyt
Copy link

TypeScript 2.4 will introduce native string enums

https://github.com/Microsoft/TypeScript/wiki/What's-new-in-TypeScript#string-enums

Will this library be obsolete? If not, what are the pros/cons of this library versus TypeScript 2.4 native string enums?

thank you

@dphilipson
Copy link
Owner

Hi Benny,

I do think this library will be unnecessary for most people who are on 2.4. There are still a few reasons users may still prefer to use this library.

  • This library provides several helper functions which cannot easy be implemented for native enums. Of these, I would expect Enum.isType to be the most useful. (At least, I don't see an obvious way to implement the helper functions. I will play with it upon release).
  • Defining a native string enum involves a bit of repetition, as each value must be written twice:
    enum Color {
        RED = "RED",
        GREEN = "GREEN",
        BLUE = "BLUE"
    }
    vs
    const Color = Enum("RED", "GREEN", "BLUE");
    type Color = Enum<typeof Color>;
    If there are many values, it may be desirable to avoid the repetition and corresponding possibility for typos.

When 2.4 is released, I will add a note to the intro of the readme suggesting that users migrate to native enums and providing a link to this reasoning.

@benny-medflyt
Copy link
Author

Awesome answer, thank you!

@dphilipson
Copy link
Owner

TypeScript 2.4 released today, so as promised, the notes from above have been added to the README.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants