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?
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.
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.
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.
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.
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.
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
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 static
s. 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 enum
s or just enum
s.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With