Support for polymorphism, subclassing, interfaces, union types #382
evertheylen
started this conversation in
General
Replies: 1 comment
-
Your example with serialize<Animal>(new Human("Linus")) // --> {} It's expected behaviour to not get any other properties than the ones in Animal. This behaviour has concrete use cases. If you want to get it to work like you want, use const o = new Human();
serialize<Animal>(o, undefined, undefined, undefined, reflect(getClassTypeFromInstance(o))); |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello!
I am working on a project that, similar to deepkit, stresses isomorphic typescript. In particular, I am also working on (de)serialization, RPC, and ORM features. I must say that this looks like a fantastic library.
I've been browsing the documentation for deepkit and one thing stood out to me: the lack of support for polymorphism in its various forms. Consider the following class hierarchy:
If I serialize a
Lion
as anAnimal
, it loses the special behaviour. If I serialize aHuman
as anAnimal
, it even loses data. For example:The above example is contrived as it is trivial to just call
serialize<Human>(...)
. But if we storeAnimal
objects in an array or some other container, you can have a mix ofLion
andHuman
and are no longer able to specify the exact type as it could be both.Now I do know many people are against subclassing, and would suggest just not doing it. Two points:
First, the problem is a bit wider than just subclassing, it also applies to interfaces or any time a union type (
A | B
) comes into play. Consider that the following will give two strings instead of a Date and a string:Second, polymorphism is part of Javascript and therefore Typescript, you can't just drop a part of it. I think there is an (implicit?) claim that deepkit "Just Works" for any correctly typed code. However, the above examples fail to provide "real" isomorphism of objects as information is lost when passing data between frontend/backend.
I am very curious to hear about how deepkit plans to face these challenges. I think not supporting them could be a valid option if you warn the user about it, but actual implementations certainly have my interest 😃
Beta Was this translation helpful? Give feedback.
All reactions