JohnWeisz/TypedJSON

Does typedJSON support typescript union types ?

Closed this issue · 3 comments

Everything is in the title, I'm talking about | types not about discriminated unions

Hey @LifeIsStrange, this is not really possible at the moment. We are using reflect-metadata for getting the type of a property and currently it returns Object if you have a union type.

However, you may be able to do something depending on your case. You can add your own serialiser/deserialiser. For simple types (string|number|object) you can just use an identity function for that. For classes you could write an if statement (ex. on a property occurring on only one of the types) and recursively call TypedJSON. You can also configure a typeResolver that would resolve the actual type in a similar way; here you would also probably be interested in adjusting the typeEmitter to not emit a type hint in the output.

Hope that helps

Hey @JohnWeisz -- I'm not sure I completely understand what you mean by adding a serializer/deserializer. Could you attach a snippet of code where you do this? I couldn't find how to add a custom serializer/deserializer to override TypedJSON's default behavior in the README.md. Thank you!

Hey @samstronghammer, sorry for such a delay. I have updated the documentation with a description and an example. You can also take a look at the tests to see a complete example.