Suggestion
Fields marked with the "private" keyword in child classes should have their initialization (if an inline assignment is provided) occur before the super call in the emitted JS.
such as:
private _field: string = 'Test';
🔍 Search Terms
child private field assignment initialization before super call
✅ Viability Checklist
My suggestion meets these guidelines:
📃 Motivating Example
Currently I am extending a class from an imported npm library, which I would rather not touch the code of directly, whether through a fork or local code edit. The issue being that I need to override methods and accessors from the base class in the library while utilizing my own child class's private fields in these methods and accessors. The accessors and methods I am overriding are called directly in the parent class's constructor. This is an issue because my field initializations do not occur until after the super call in the emitted code. The only way around this would to check if the field value is set in the getter of the property, and assign the default value, while also assigning the default value inline at the definition or in the constructor after the super call, which is necessary as i have the '"strictPropertyInitialization": true' TS config compiler option set.
See the simple typescript playground example outlining the issue (open browser console to see the error)
Simple TS Playground Example Using TS v4.1.3
💻 Use Cases
As said above in the motivating example, more accurately, I am using an angular component library and extending a component to use my stateChanges RXjs Subject to detect changes inside relevant display related property setters, in order to refresh the view with up-to-date values (for reference I am using the OnPush change detection strategy).
Having to work around this by both checking for set values in getters and assigning a default value elsewhere, is quite ugly and inconvenient.
If there is an easier/ cleaner workaround, great, but for now I think the best would be to have child private field assignments occur before super calls. This would not cause any adverse side-effects that I can think of with existing typescript code, as derived classes cannot have same named property declarations.
Suggestion
Fields marked with the "private" keyword in child classes should have their initialization (if an inline assignment is provided) occur before the super call in the emitted JS.
such as:
🔍 Search Terms
child private field assignment initialization before super call
✅ Viability Checklist
My suggestion meets these guidelines:
📃 Motivating Example
Currently I am extending a class from an imported npm library, which I would rather not touch the code of directly, whether through a fork or local code edit. The issue being that I need to override methods and accessors from the base class in the library while utilizing my own child class's private fields in these methods and accessors. The accessors and methods I am overriding are called directly in the parent class's constructor. This is an issue because my field initializations do not occur until after the super call in the emitted code. The only way around this would to check if the field value is set in the getter of the property, and assign the default value, while also assigning the default value inline at the definition or in the constructor after the super call, which is necessary as i have the '"strictPropertyInitialization": true' TS config compiler option set.
See the simple typescript playground example outlining the issue (open browser console to see the error)
Simple TS Playground Example Using TS v4.1.3
💻 Use Cases
As said above in the motivating example, more accurately, I am using an angular component library and extending a component to use my stateChanges RXjs Subject to detect changes inside relevant display related property setters, in order to refresh the view with up-to-date values (for reference I am using the OnPush change detection strategy).
Having to work around this by both checking for set values in getters and assigning a default value elsewhere, is quite ugly and inconvenient.
If there is an easier/ cleaner workaround, great, but for now I think the best would be to have child private field assignments occur before super calls. This would not cause any adverse side-effects that I can think of with existing typescript code, as derived classes cannot have same named property declarations.