How to: .NET enums to and from JS/TS strings #2015
BaconSoap
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I couldn't find a consolidated guide (especially on the string->enum parsing) and wanted to share in case others find it helpful.
I'm using jint to enable moddable logic in my game, writing both core systems and mods in typescript on top of .NET objects. I also use typegen to keep the typescript models in sync. Instead of ints, I prefer to use string literals for enums in my JSON/REST design, backed by string unions in TS.
To get this setup to work, I:
IObjectConverter
to serialize enums to JS strings, based on How to pass CLR enums as strings? #267 (comment)DefaultTypeConverter
, intercepting conversions from JS strings to .NET enums, passing through to the default behavior for everything elseGenerationSpec
subclass:AddEnum<IncludeExclude>(asUnionType: true);
I wrapped it all up in this gist to make it easier to share/comment.
Beta Was this translation helpful? Give feedback.
All reactions