Proposal: Follow program flow in constructor when strictPropertyInitialization = true
See original GitHub issueSearch Terms
constructor strictPropertyInitialization flow
Suggestion
TypeScript should follow unconditional function calls in constructor when checking for strict property initialization.
Use Cases
It is a common pattern to be able to reset objects after construction and, thus, to transfer object initialization to a separate routine:
class C
{
private p: number;
public constructor() { Initialize() }
public Initialize() { p = 0; }
}
Currently, TypeScript doesn’t consider functions being unconditionally called from within a constructor function when checking for strict property initialization. The above code yields a compiler error.
I propose TypeScript to be able to analyse code flow and recognize property initialization that’s performed in functions unconditionally called by the constructor function.
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, etc.)
- This feature would agree with the rest of TypeScript’s Design Goals.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:14
- Comments:10 (2 by maintainers)
Top Results From Across the Web
Strict Property Initialization in TypeScript - Marius Schulz
TypeScript 2.7 introduced a new compiler option for strict property initialization checks in classes.
Read more >Documentation - Classes - TypeScript
The strictPropertyInitialization setting controls whether class fields need to ... is true , class fields are initialized after the parent class constructor ......
Read more >Bulletproof Angular. Angular strict mode explained
To enable it you specify strict: true in a tsconfig.json : ... author of the code to initialize all the class properties in...
Read more >interface getter setter typescript - Lena's Italian Restaurant
This also means that under --strictPropertyInitialization, TypeScript can ... TypeScript code must not use the Array() constructor, with or without new.
Read more >Announcing TypeScript 4.7 Beta - Microsoft Developer Blogs
For example, let's say you have the following code today: ... Under TypeScript 4.7, --strictPropertyInitialization reports an error telling ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Without that feature, the usage of
strictPropertyInitialization
is questionable for me. We’ve just enabled it in a quite large project and we had to move code from our private initialization methods back to the constructor because TS wasn’t able to properly recognize initialization. This made code less readable, which was not my intention at all.I just stumbled over a similar issue, where the base class is abstract and generic:
… results in:
You may call this a design flaw as a constructor like:
… may solve the problem. Though, basically, it would be plain correct to omit this redundant constructor.
This error should not occur in an abstract class. It should, however, occur in a non-abstract derived class if all base class constructors and the constructor of the first non-abstract derived class don’t set the protected field.