Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enums vs Constant differences on Typescript

If I need to use const variables I use this approach since it has a type safety.

export class LocalStorage {
    static USER_INFO = "user-info";
    static PROFILE_INFO = "profile-info";   
}

But it seems we can use string enums like so:

export enum LocalStorage {
    USER_INFO = "user-info";
    PROFILE_INFO = "profile-info";   
 } 

What is the difference between these 2 approaches?

like image 796
Sampath Avatar asked Oct 16 '19 14:10

Sampath


People also ask

Is enum a type or constant TypeScript?

In TypeScript, enums, or enumerated types, are data structures of constant length that hold a set of constant values. Each of these constant values is known as a member of the enum.

Should enums be used in TypeScript?

Enums are just one useful way to organize code in TypeScript. Here are some reasons why enums come in handy: With enums, you can create constants that you can easily relate to, making constants more legible. Developers have the freedom to create memory-efficient custom constants in JavaScript.

What is the difference between enum and constant in JavaScript?

No, enum is a type that defines named values, a constant variable or value can be any type. You use an enum variable to hold a value of the enum type, you use a const to define a variable that cannot change and whose value is known at compile time. Please Sign up or sign in to vote.

What can I use instead of enums TypeScript?

Alternative 1: String Unions This solution uses a union of string literal types. TS gives great autocompletes for these, and gives helpful error messages if you make type errors. Pros of the approach: Both the definition and use sites are readable and no-boilerplate.


3 Answers

I agree with @VLAZ, the best approach can definitely vary. Probably the most significant perk of using an enum is being able to improve type-safety of your functions.

export enum LocalStorage {
    USER_INFO = "user-info",
    PROFILE_INFO = "profile-info"
} 

function doSomething(l: LocalStorage) {
}

doSomething("foo"); // error, "foo" is not LocalStorage
doSomething("user-info"); // error, "user-info" is not LocalStorage
doSomething(LocalStorage.USER_INFO); // compiles

Overall, enums offer improved type safety (where they make sense), and extending them with new members is generally simpler.

like image 117
CollinD Avatar answered Oct 18 '22 03:10

CollinD


enum is logical grouping of your constants. Let's say you want to use different color. Then you make color enum consist of all colors value defined. Lets say accountType which consist value of current, saving, loan, recurring. Its logical grouping. Constant you can define for anything.

Now you have to make sure that your enum name has anything to do with logical grouping?

In Your case Enum name is LocalStorage but values underneath does not justify as enum

like image 6
vipul patel Avatar answered Oct 18 '22 05:10

vipul patel


While both approaches get the job done for simple scenarios. It's always good practice to use constructs for their intended purposes.

Your class may inadvertently evolve into something more than just holding statics. Leading to an amalgamation of concerns. For instance, in the future you may think it necessary to add CRUD functions for local storage, indexedDB, or cookies.

Then the question comes to using const enums or just enums.

If you need the compiled enum at run time, then use a regular TS enum. Otherwise, a const enum exists just to help with type-safety, less generated code, and minimised access indirection but without run-time lookup values.

like image 5
ethane Avatar answered Oct 18 '22 04:10

ethane