question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support of string-based enums

See original GitHub issue

I know this kind of a limitation of proto protocol, but would be good to have the ability to have string-based enums in TS.

enum EmploymentStatus {
  unemployed = 0;
  employed = 1;
  student = 2;
  retired = 3;
}

it intended to be in the TS:

enum EmploymentStatus = {
  unemployed = 'unemployed';
  employed = 'employed';
  student = 'student';
  retired = 'retired';
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
thesayyncommented, Jul 1, 2021

also, using oneof fileds could be an option.

message EmploymentStatus {
  oneof name {
      bool unemployed = 0;
      bool employed = 1;
      bool student = 2;
      bool retired = 3;
  }
}


```typescript
const status = new EmploymentStatus({unemployed: true});
status.name // "unemployed" | "employed" | "student" | "retired"

since one of fields can present one at a time, this would act like an enum and we wouldn’t have to worry about wire format or interoperability as this is a normal way of using the language.

0reactions
tomasweigenastcommented, Aug 21, 2022

@thesayyn Here it says the ENUM specification: https://developers.google.com/protocol-buffers/docs/proto3#json It must be a string, not an integer. If you use Protobuf with dart, it will serialize enums as strings, and this plugin will not understand that.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript string enums, and when and how to use them
No matter how much you know about TypeScript enums, this guide covers all the best practices for using string-based enums in production.
Read more >
Handbook - Enums - TypeScript
String enums allow you to give a meaningful and readable value when your code runs, independent of the name of the enum member...
Read more >
String Enums in C#: Everything You Need to Know
Since C# doesn't support enum with string value, in this blog post, we'll look at alternatives and examples that you can use in...
Read more >
typescript - Create an enum with string values - Stack Overflow
Enums in TypeScript are number based. You can use a class with static members though: class E { static hello = "hello" ...
Read more >
String-based enums · Issue #2849 · dotnet/csharplang - GitHub
The primary value of string-based enums is that they are extensible without changing the type definition: OSPlatform platform = (OSPlatform)" ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found