Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Proposal: Follow program flow in constructor when strictPropertyInitialization = true

See original GitHub issue

Search Terms

constructor strictPropertyInitialization flow


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.


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:open
  • Created 5 years ago
  • Reactions:14
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

fetiscommented, Apr 23, 2021

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.

SetTrendcommented, Aug 21, 2019

I just stumbled over a similar issue, where the base class is abstract and generic:

export default abstract class A<T>
  protected _state: T;

… results in:

TS2564: Property '_state' has no initializer and is not definitely assigned in the constructor.

You may call this a design flaw as a constructor like:

protected constructor(state: T)
  this._state = state;

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found