Discriminated Unions
Closed this issue · 2 comments
Hi @JohnWeisz , thanks very much for this library, it's very nice.
I'm considering using this library to serialized/deserialize a Typescript data structure which contains a lot of discriminated unions, and I don't see unions mentioned anywhere in the README or the Issues for this library.
Before I dive in and try this, do you have any advice for me? Do you know if TypedJSON can work for serializing discriminated union types? Maybe I can make it work with decorator factories?
Hey @jamesdbrock
Thanks, glad you like it, @Neos3452 also did a lot of contribution lately.
Unfortunately, advanced TypeScript types that are based on interfaces in any way are not supported here, because at runtime, TypedJSON requires type metadata, which is only provided in an extremely limited fashion by TypeScript, and is always simply just Object
for any kind of interface-based type. Ultimately, it comes down to class objects at runtime (also called constructor functions prior to ES6).
The solution would be to allow manually marking up the type tree through some configuration in @jsonObject
decorators (or similar), but the whole point of TypedJSON is to be as plug-and-play as possible, without having to manually construct the JSON schema.
The closest you can achieve is using superclasses to group class types in a somewhat limited fashion, but it's not going to provide the most advanced type constructs possible in TypeScript.
@jamesdbrock so, did you manage to get a solution, I share a similar need?