I suggest you ...

Typescript and C# Enums: option for string literal types

We already proposed https://servicestack.uservoice.com/forums/176786-feature-requests/suggestions/10105086-add-support-for-non-ambient-typescript-module-gene

We are using this and it works well, but unfortunately there is one issue: enums!

We serialize the enums as strings and not integers.

In typescript world this all works fine, the enums are in the generated DTO's and the compiler is happy. But on the wire there is the Enum as a string instead of an integer which typescript can't map back to the typed dto. so all my typescript code doesn't work anymore.

We end up doing:

myAnimal.AnimalType.ToString() === dtos.AnimalType[dtos.AnimalType.Rabbit]

which is working but doesn't feel "right". From a typescript point of view we loose a lot we could actually get from an enum. So we are even thinking of going back to normal strings at least on js/ts side.

In TS 1.8 there is "string literal types" comming: https://github.com/Microsoft/TypeScript/wiki/What's-new-in-TypeScript#string-literal-types

We propose to have an option in the SS typescript type generator to generate string literal types for C# enums instead of creating typescript enums. This could be a simple query param which people could use if they want to.

30 votes
Vote
Sign in
Check!
(thinking…)
Reset
or sign in with
  • facebook
  • google
    Password icon
    Signed in as (Sign out)
    You have left! (?) (thinking…)
    Tobias Zürcher shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

    0 comments

    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      Signed in as (Sign out)
      Submitting...

      Feedback and Knowledge Base