Ensure Initialization of Class Instance Properties in TypeScript

Marius Schulz
InstructorMarius Schulz

Share this video with your friends

Send Tweet

We're going to take a look at TypeScript's strictPropertyInitialization compiler option and how it helps us prevent using uninitialized class instance properties. If the strictPropertyInitialization flag is enabled, the type checker verifies that each instance property declared in a class either

  • has a type that includes undefined,
  • has an explicit initializer, or
  • is definitely assigned to in the constructor.

The strictPropertyInitialization option is part of the family of compiler options that is enabled automatically when the strict flag is set. As with all the other strict compiler options, you can set strict to true and selectively opt out of strict property initialization checks by setting strictPropertyInitialization to false.

Additional Reading