Make TypeScript Class Usage Safer with Strict Property Initialization

InstructorRares Matei

Share this video with your friends

Send Tweet

By setting the strictPropertyInitialization flag in the .tsconfig file, TypeScript will start throwing errors unless we initialize all properties of classes on construction. We’ll explore how you can fix the errors by assigning to them directly or in the constructor body. And if you can’t initialize directly but you’re sure it will be assigned to at runtime by a dependency injection library, you can use the definite assignment assertion operator to ask TypeScript to ignore that property.

Pavlo Kochubei
~ 5 years ago

Great tip. You can also use strict: true, which is a shorthand for enabling: noImplicitAny, noImplicitThis, alwaysStrict, strictBindCallApply, strictNullChecks, strictFunctionTypes, strictPropertyInitialization.