Subclass serialization is probably best done by tagging a structure with the name of its constructor, like { tag: string; value: S; }, where S is the representational type in Schema<T, S> for the subclass. This could be easy to track with a decorator, since every constructor implicitly extends { name: string; }. A few potential difficulties:
This requires a being able to do a lookup from names to subclasses. Either this means listing out every instance in the superclass, which is inflexible (and could fail at runtime due to circular dependency), or it means figuring out a way to do some very clever metaprogramming with decorator metadata.
If the schemas are kept as static members on a type, the superclass' schema would have a different type than the subclasses', which would be a compiler error, since typeof Sub does not extend typeof Super, as TS requires.
Subclass serialization is probably best done by tagging a structure with the name of its constructor, like
{ tag: string; value: S; }
, whereS
is the representational type inSchema<T, S>
for the subclass. This could be easy to track with a decorator, since every constructor implicitly extends{ name: string; }
. A few potential difficulties:typeof Sub
does not extendtypeof Super
, as TS requires.