24

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. 
Jeffrey Juarez
  • 271
  • 2
  • 3
  • 7
  • So, you're asking how to convert a number to a string? – Heretic Monkey May 30 '18 at 19:23
  • Possible duplicate of [What's the best way to convert a number to a string in JavaScript?](https://stackoverflow.com/questions/5765398/whats-the-best-way-to-convert-a-number-to-a-string-in-javascript) – Heretic Monkey May 30 '18 at 19:28
  • 1
    Ok, I thought it would require doing something like RecordStatus[RecordStatus.Submitted]. – Jeffrey Juarez May 30 '18 at 19:35
  • 1
    @JeffreyJuarez That is if you want to get the string `Submitted`. See https://www.typescriptlang.org/play/#src=enum%20RecordStatus%20%7B%0A%20%20%20%20CancelledClosed%20%3D%20102830004%2C%0A%20%20%20%20Completed%20%3D%20102830003%2C%0A%20%20%20%20InProgress%20%3D%20102830002%2C%0A%20%20%20%20ShoppingCart%20%3D%20102830005%2C%0A%20%20%20%20Submitted%20%3D%20102830001%2C%0A%20%20%20%20Unordered%20%3D%20102830000%0A%7D%0A%0Aconsole.log(RecordStatus.Submitted)%0Aconsole.log(RecordStatus%5BRecordStatus.Submitted%5D) – Ruan Mendes May 31 '18 at 03:14

1 Answers1

23

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

kshetline
  • 12,547
  • 4
  • 37
  • 73
  • 3
    You could always use string enums too, especially if you're going to always want to use the string value, and you're going to explicitly assign a value to each enum. – kshetline May 30 '18 at 20:35