Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert a typescript enum value to string

Tags:

typescript

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. 
like image 867
Jeffrey Juarez Avatar asked May 30 '18 19:05

Jeffrey Juarez


People also ask

How do I convert an enum to a string?

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.

Can enum be string TypeScript?

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.

Can enum values be strings?

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.

Can we change enum values in TypeScript?

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.


1 Answers

You can just use RecordStatus.Submitted.toString().

like image 185
kshetline Avatar answered Sep 28 '22 01:09

kshetline