I have an enum in typscript. I need to convert the value of the enum to string so that I can pass this to an api endpoint. How do I do this? Thanks.
enum RecordStatus {
CancelledClosed = 102830004,
Completed = 102830003,
InProgress = 102830002,
ShoppingCart = 102830005,
Submitted = 102830001,
Unordered = 102830000
}
example:
var submittedStrValue = RecordStatus.Submitted // I want to get "10283001" (note the string value rather than the int value.
There are two ways to convert an Enum to String in Java, first by using the name() method of Enum which is an implicit method and available to all Enum, and second by using toString() method.
Enums are one of the few features TypeScript has which is not a type-level extension of JavaScript. Enums allow a developer to define a set of named constants. Using enums can make it easier to document intent, or create a set of distinct cases. TypeScript provides both numeric and string-based enums.
Given those limitations, the enum value alone is not suitable for human-readable strings or non-string values. In this tutorial, we'll use the enum features as a Java class to attach the values we want.
An enum is usually selected specifically because it is immutable - i.e. you would never want the values to change as it would make your application unpredictable.
You can just use RecordStatus.Submitted.toString()
.
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