-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Open
Labels
In DiscussionNot yet reached consensusNot yet reached consensusSuggestionAn idea for TypeScriptAn idea for TypeScript
Description
Search 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.
iamchathu, 1valdis, johnameyer, beauxq, kirosc and 21 more
Metadata
Metadata
Assignees
Labels
In DiscussionNot yet reached consensusNot yet reached consensusSuggestionAn idea for TypeScriptAn idea for TypeScript