-
-
Notifications
You must be signed in to change notification settings - Fork 535
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
typescript: field initialization order in derived classes #625
Labels
Comments
This is pretty serious, thanks for reporting. I think only your last option would be viable... but it would look like that:
Wouldn't this produce much more code? |
RicoSuter
added a commit
that referenced
this issue
Feb 19, 2018
Can you have a look at my commit? Does it look good? Do you think the issue is fixed with this change? |
Based on this suggestion, as a workaround I've gone with this
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
With c# code such as
the generated typescript will look something like
The BarChild constructor causes incorrect data initialization. The rules for typescript classes (and eventually javascript classes once emcascript adds property initializers) is as follows:
applying these rules to the above, what happens is the base class Bar initialized properties are run (there aren't any), the Bar constructor runs (which copies the properties from data into the new instance), the derived class (BarChild) properties are initialized (in this case,
values
is set to[]
overwriting the value copied from data), and finally the derived class constructor runs (which does nothing). Therefore, no matter what you pass to theBarChild
constructor,values
will always be the empty list since the initializer overwrites anything from the base class constructor.You can read more about this at microsoft/TypeScript#10634 or microsoft/TypeScript#1617 or microsoft/TypeScript#13525 Also, https://stackoverflow.com/questions/43595943/why-are-derived-class-property-values-not-seen-in-the-base-class-constructor/43595944 contains a discussion why this is the case.
I was looking through https://github.com/RSuter/NJsonSchema/blob/master/src/NJsonSchema.CodeGeneration.TypeScript/Templates/Class.liquid but I don't see an easy way of fixing... the problem is everything about the base class runs before the derived class even gets a chance to do anything. One option would be to not use property initialization at all. Instead, the BarChild constructor could contain code as
The text was updated successfully, but these errors were encountered: