-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Inherited Constructors execute before sub class field initializations #13525
Comments
duplicate of #10634? |
Sounds like it, didn't come up in my search. Is there any way to achieve this functionality (calling code after the constructors finish) without changing the order of construction? |
Nope. General pattern here is to have some convention like calling |
Understood, it just seems like an anti-pattern to have, what is essentially, a secondary constructor to compensate for the initialization of fields. I'm toying with using an inherited static method to act as the constructor (e.g. |
So, playing around with some solutions, what I'm looking at is: class BaseSchema {
static new<T>(this: new () => T, data: any): T {
return BindUtil.bindSchema(this, new this(), data);
}
} This then allows for class Person extends BaseSchema {
name: string = 'bob';
}
let person:Person = Person.new({}); |
TypeScript Version: 2.1.5
Code
Expected behavior:
new SubType({ name : 'test' }).name == 'test'
Actual behavior:
new SubType({ name : 'test' }).name == 'basic'
The sub class field initialization executes after the base class constructor executes. I'm not sure if this is 100% desirable but I can understand the reasoning for it. If this is unchangeable, I would look for some way to execute the
bindToSchema
function after the sub class field initialization has occurred (barring making every use of the constructors callbindToSchema
manually).The text was updated successfully, but these errors were encountered: