Strict Property Initialization

This lesson introduces another compiler flag, `strictPropertyInitialization`, which brings more type safety to code that uses classes.

Definition

The flag we’re talking about is called strictPropertyInitialization. As usual, let’s look at the definition from the documentation. I think it’s rather self-explanatory.

Ensure non-undefined class properties are initialized in the constructor. This option requires – strictNullChecks be enabled in order to take effect.

Enabling the flag will force you to always initialize class properties or make it explicit in the type annotation that the property can be undefined.

Fixing the error

In the piece below, the header property produces an error because it’s initialized neither in-place nor in the constructor.

Get hands-on with 1200+ tech skills courses.