Skip to content

Private field member value assignment's of child classes should occur before super call #42325

@SLGShark6

Description

@SLGShark6

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:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

📃 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DeclinedThe issue was declined as something which matches the TypeScript visionSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions